Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 two way binding using ngModel is not working

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); 
like image 997
yajra Avatar asked Jul 25 '15 07:07

yajra


People also ask

How does ngModel work in Angular 2?

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.

How do I enable ngModel?

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.

What does [( ngModel )] mean?

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.

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

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.


1 Answers

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 to FormsModule, that's why you should import the FormsModule from @angular/forms module inside imports metadata option of AppModule(NgModule). Thereafter you can use ngModel 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

like image 118
Pankaj Parkar Avatar answered Oct 25 '22 08:10

Pankaj Parkar