Can't bind to 'ngModel' since it isn't a know property of the 'input' element and there are no matching directives with a corresponding property
Note: im using alpha.31
import { Component, View, bootstrap } from 'angular2/angular2' @Component({ selector: 'data-bind' }) @View({ template:` <input id="name" type="text" [ng-model]="name" (ng-model)="name = $event" /> {{ name }} ` }) class DataBinding { name: string; constructor(){ this.name = 'Jose'; } } bootstrap(DataBinding);
The Angular uses the ngModel directive to achieve the two-way binding on HTML Form elements. It binds to a form element like input , select , selectarea . etc. Internally It uses the ngModel in property, binding to bind to the value property and ngModelChange which binds to the input event.
Use the ngModel selector to activate it. It accepts a domain model as an optional Input . If you have a one-way binding to ngModel with [] syntax, changing the domain model's value in the component class sets the value in the view.
ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form.
To fix Can't bind to 'ngModel' since it isn't a known property of 'input' error in Angular applications we have to import FormModule in app. module. ts file. If you are using FormBuilder class to create reactive form we have to import ReactiveFormsModule as well to avoid below error.
Angular has released its final version on 15th of September. Unlike Angular 1 you can use ngModel
directive in Angular 2 for two way data binding, but you need write it in a bit different way like [(ngModel)]
(Banana in a box syntax). Almost all angular2 core directives doesn't support kebab-case
now instead you should use camelCase
.
Now
ngModel
directive belongs toFormsModule
, that's why you shouldimport
theFormsModule
from@angular/forms
module insideimports
metadata option ofAppModule
(NgModule). Thereafter you can usengModel
directive inside on your page.
app/app.component.ts
import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: `<h1>My First Angular 2 App</h1> <input type="text" [(ngModel)]="myModel"/> {{myModel}} ` }) export class AppComponent { myModel: any; }
app/app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule, FormsModule ], //< added FormsModule here declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
app/main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; const platform = platformBrowserDynamic(); platform.bootstrapModule(AppModule);
Demo Plunkr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With