Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’?
I have this simple input in my component which uses [(ngModel)]
:
<input type="text" [(ngModel)]="test" placeholder="foo" />
And I get the following error when I launch my app, even if the component is not displayed.
zone.js:461 Unhandled Promise rejection: Template parse errors: Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’.
Here is the component.ts:
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";
}
For using
[(ngModel)]
in Angular 2, 4 & 5+, you need to import FormsModule from Angular form…Also, it is in this path under forms in the Angular repository on GitHub:
Probably this is not a very pleasurable for the AngularJS developers as you could use ng-model everywhere anytime before, but as Angular tries to separate modules to use whatever you’d like you to want to use at the time, ngModel is in FormsModule now.
Also, if you are using ReactiveFormsModule it needs to import it too.
So simply look for app.module.ts and make sure you have
FormsModule
imported in…Also, these are the current starting comments for Angular4
ngModel
in FormsModule:If you’d like to use your input, not in a form, you can use it with
ngModelOptions
and make standalone true…Yes, that’s it. In the app.module.ts file, I just added: