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";
}
You need to import the FormsModule
Open app.module.ts
and add line
and
ngModel is coming from FormsModule.There are some cases when you can receive this kind of error:
(In some version I faced this problem) You have imported correctly the FormsModule but the problem is on the input HTML tag. You must add the name tag attribute for input and the object bound name in [(ngModel)] must be the same as the name into the name attribute
Throwing in this might help someone.
Assuming you have created a new NgModule, say
AuthModule
dedicated to handling your Auth needs, make sure to importFormsModule
in that AuthModule too.If you'll be using the
FormsModule
ONLY in theAuthModule
then you wouldn't need to import theFormModule
IN the defaultAppModule
So something like this in the
AuthModule
:Then forget about importing in
AppModule
if you don't use theFormsModule
anywhere else.If someone is still getting errors after applying the accepted solution, it could be possibly because you have a separate module file for the component in which you want to use the ngModel property in input tag. In that case, apply the accepted solution in the component's module.ts file as well.
When I first did the tutorial, main.ts looked slightly different from what it is now. It looks very similar, but note the differences (the top one is correct).
Correct:
Old tutorial code:
In the module you are willing to use ngModel you have to import FormsModule