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);
As per Angular2 final, you do not even have to import
FORM_DIRECTIVES
as suggested above by many. However, the syntax has been changed as kebab-case was dropped for the betterment.Just replace
ng-model
withngModel
and wrap it in a box of bananas. But you have spilt the code into two files now:app.ts:
app.module.ts:
Key Points:
ngModel in angular2 is valid only if the FormsModule is available as a part of your AppModule.
ng-model
is syntatically wrong.So, to fix your error.
Step 1: Importing FormsModule
Step 2: Add it to imports array of your AppModule as
Step 3: Change
ng-model
as ngModel with banana boxes asNote: Also, you can handle the two way databinding separately as well as below
instead of ng-model you can use this code:
inside your app.component.ts
In the app.module.ts
Later in the @NgModule decorator's import:
Angular 2 Beta
This answer is for those who use Javascript for angularJS v.2.0 Beta.
To use
ngModel
in your view you should tell the angular's compiler that you are using a directive calledngModel
.How?
To use
ngModel
there are two libraries in angular2 Beta, and they areng.common.FORM_DIRECTIVES
andng.common.NgModel
.Actually
ng.common.FORM_DIRECTIVES
is nothing but group of directives which are useful when you are creating a form. It includesNgModel
directive also.The answer that helped me: The directive [(ngModel)]= not working anymore in rc5
To sum it up: input fields now require property
name
in the form.