Can't bind to 'ngModel' since it isn&#

2019-01-01 14:25发布

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 app works.

zone.js:461 Unhandled Promise rejection: Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("
    <div>
        <label>Created:</label>
        <input  type="text" [ERROR ->][(ngModel)]="test" placeholder="foo" />
    </div>
</div>"): InterventionDetails@4:28 ; Zone: <root> ; Task: Promise.then ; Value: 

I'm looking at the Hero plunker but I don't see any difference.

Here is the component file:

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Intervention } from '../../model/intervention';

@Component({
    selector: 'intervention-details',
    templateUrl: 'app/intervention/details/intervention.details.html',
    styleUrls: ['app/intervention/details/intervention.details.css']
})

export class InterventionDetails
{
    @Input() intervention: Intervention;

    public test : string = "toto";
}

22条回答
永恒的永恒
2楼-- · 2019-01-01 14:56

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!

查看更多
人间绝色
3楼-- · 2019-01-01 14:58

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.

查看更多
心情的温度
4楼-- · 2019-01-01 14:58

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.

查看更多
时光乱了年华
5楼-- · 2019-01-01 14:59

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 { }
查看更多
登录 后发表回答