'The directive [(ngModel)]= not working anymore in rc5
ngModel is throwing exceptions, this worked fine in rc4
<input [(ngModel)]="par.name" placeholder="name" />
These are the exceptions:
[email protected]?main=browser:260 Uncaught EXCEPTION: Error in ./CommunityListComponent class CommunityListComponent - inline template:10:27 ORIGINAL EXCEPTION: No value accessor for form control with unspecified name ORIGINAL STACKTRACE: Error: No value accessor for form control with unspecified name
Solution 1:[1]
can also solve by importing FormsModule into your bootstrap module, then it will be available to all components.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from 'app/components/app';
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
Solution 2:[2]
Now you need to set the name on input. For example;
<input [(ngModel)]="par.name" **name="name"** placeholder="name" />
And all directive must be set on *.module.ts.
Solution 3:[3]
Try like this-
import { Component, OnInit } from '@angular/core';
import { FORM_DIRECTIVES } from '@angular/common';
@Component({
selector: 'my-app',
template: `<h1>My First Angular 2 App</h1>
<input [(ngModel)]="employee.empName">
`,
directives: [FORM_DIRECTIVES]
})
export class AppComponent {
employee = { empName: 'Sanket', email: '', phone: '', address:'' };
ngOnInit() {
}
}
This is working for me in RC5.
Reference - https://angular.io/docs/ts/latest/api/common/index/NgModel-directive.html
Solution 4:[4]
Two things you need to do while using two-way binding syntax in forms in RC5.
Include FormsModule in your app.module.ts
Use name attribute in your html input tags like this:
<input type="text" name="name" [(ngModel)]="par.name" />
That's all.
Solution 5:[5]
At the app.module.ts
import { FormsModule } from '@angular/forms';
Then at @NgModule(){imports:[FormsModule]} with other staff
Solution 6:[6]
You need to tell it explicitly that changes are being made. Please observe:
- import
{ChangeDetectorRef} - declare its object in constructor.
this.object.detectChanges();in function where changes are being made
Solution 7:[7]
There's another simple substitute as follows:
item: string = "";
<input name= "item" (input)= "item=$event.target.value">
<h1>{{item}}<h1>
Solution 8:[8]
You can resolve it by adding formControlName like this <input [(ngModel)]="par.name" placeholder="name" formControlName="name"/>
Here I attach an image when I use [(ngModel)] in <input/> without formControlName
I hope It helps you...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | systemjsFan |
| Solution 2 | Victor Hugo |
| Solution 3 | Sanket |
| Solution 4 | wmnitin |
| Solution 5 | |
| Solution 6 | Petr Hejda |
| Solution 7 | |
| Solution 8 | Neel Rathod |

