Can't bind to 'ngModel' since it isn't a known property of 'input'

前端 未结 30 1049
予麋鹿
予麋鹿 2020-11-22 12:44

I\'ve got the following error when launching my Angular app, even if the component is not displayed.

I have to comment out the so that my

相关标签:
30条回答
  • 2020-11-22 13:00

    Yes that's it, in the app.module.ts, I just added :

    import { FormsModule } from '@angular/forms';
    
    [...]
    
    @NgModule({
      imports: [
        [...]
        FormsModule
      ],
      [...]
    })
    
    0 讨论(0)
  • 2020-11-22 13:00

    Simple Soultion : In app.module.ts

    Example 1

    import {FormsModule} from "@angular/forms";  
    // add in imports 
    
    imports: [
     BrowserModule,
     FormsModule
     ],
    

    Example 2

    If you want to use [(ngModel)] then you have to import FormsModule in app.module.ts

    import { FormsModule  } from "@angular/forms";     
    @NgModule({
      declarations: [
        AppComponent, videoComponent, tagDirective, 
      ],
      imports: [
        BrowserModule,  FormsModule
    
      ],
      providers: [ApiServices],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    
    0 讨论(0)
  • 2020-11-22 13:02

    If you need to use [(ngModel)] first do you need to import FormsModule in app.module.ts and then add in a list of imports. Something like this:

    app.module.ts

    1. import import {FormsModule} from "@angular/forms";
    2. add in imports imports: [ BrowserModule, FormsModule ],

    app.component.ts

    1. Example: <input type="text" [(ngModel)]="name" >
    2. and then <h1>your name is: {{name}} </h1>
    0 讨论(0)
  • 2020-11-22 13:02

    In the module you are willing to use ngModel you have to import FormsModule

    import { FormsModule } from '@angular/forms';
    
    @NgModule({
      imports: [
        FormsModule,
      ],
    
    })
    export class AbcModule { }
    
    0 讨论(0)
  • 2020-11-22 13:03

    Import the FormsModule in those modules where you want to use the [(ngModel)]

    0 讨论(0)
  • 2020-11-22 13:03

    I know this question is about Angular 2, but I am on Angular 4 and none of these answers helped.

    In Angular 4 the syntax needs to be

    [(ngModel)]
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题