8
votes

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

7

7 Answers

12
votes

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 { }
5
votes

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.

3
votes

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

2
votes

At the app.module.ts

import { FormsModule } from '@angular/forms'; 

Then at @NgModule(){imports:[FormsModule]} with other staff

1
votes

Two things you need to do while using two-way binding syntax in forms in RC5.

  1. Include FormsModule in your app.module.ts

  2. Use name attribute in your html input tags like this:

    <input type="text" name="name" [(ngModel)]="par.name" />

That's all.

0
votes

You need to tell it explicitly that changes are being made. Please observe:

  1. import {ChangeDetectorRef}
  2. declare its object in constructor.
  3. this.object.detectChanges(); in function where changes are being made
0
votes

There's another simple substitute as follows:

item: string = "";
<input name= "item" (input)= "item=$event.target.value">
<h1>{{item}}<h1>