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

前端 未结 30 960
予麋鹿
予麋鹿 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:22

    I am using Angular 7

    I have to import ReactiveFormsModule because I am using FormBuilder class to create reactive form.

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

    This is for the folks who use plain JavaScript instead of Type Script. In addition to referencing the forms script file on top of the page like below

    <script src="node_modules/@angular/forms/bundles/forms.umd.js"></script>
    

    you should also tell the the module loader to load the ng.forms.FormsModule. After making the changes my imports property of NgModule method looked like below

    imports: [ng.platformBrowser.BrowserModule, ng.forms.FormsModule],

    Happy coding!

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

    In ngModule you need to import FormsModule, because ngModel is coming from FormsModule. Please modify your app.module.ts like the below code I have shared

    import { FormsModule, ReactiveFormsModule } from '@angular/forms';
    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    @NgModule({
        declarations: [
             AppComponent,
             HomeComponent
        ],
        imports: [
             BrowserModule,
             AppRoutingModule,
             FormsModule
        ],
        bootstrap: [AppComponent]
    })
    export class AppModule { }
    
    0 讨论(0)
  • 2020-11-22 13:25
    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    import { AppComponent } from './app.component';
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        FormsModule     
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule {}
    
    0 讨论(0)
  • 2020-11-22 13:25

    Sometimes you get this error when you try to use a component from a module, which is not shared, in a different module.

    For example, you have 2 modules with module1.componentA.component.ts and module2.componentC.component.ts and you try to use the selector from module1.componentA.component.ts in a template inside module2 (e.g. <module1-componentA [someInputVariableInModule1]="variableFromHTTPRequestInModule2">), it will throw the error that the someInputVariableInModule1 is not available inside module1.componentA.component.ts - even though you have the @Input() someInputVariableInModule1 in the module1.componentA.

    If this happens, you want to share the module1.componentA to be accessible in other modules. So if you share the module1.componentA inside a sharedModule, the module1.componentA will be usable inside other modules (outside from module1), and every module importing the sharedModule will be able to access the selector in their templates injecting the @Input() declared variable.

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

    You need to import FormsModule in your root module if this component is in the root i.e. app.module.ts

    Kindly open app.module.ts

    Import FormsModule from @angular/forms

    Ex:

    import { FormsModule } from '@angular/forms';
    

    and

    @NgModule({
    imports: [
       FormsModule
    ],
    })
    
    0 讨论(0)
提交回复
热议问题