11

ngModel is throwing exceptions, this worked fine in rc4

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

These are the exceptions:

zone.js@0.6.12?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

Sanket
  • 19,295
  • 10
  • 71
  • 82
Greg L
  • 191
  • 1
  • 1
  • 5

8 Answers8

18

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 { }
systemjsFan
  • 181
  • 2
  • 1
    It is also mentioned on the tutorial itself - [https://angular.io/tutorial/toh-pt1#import-the-formsmodule](https://angular.io/tutorial/toh-pt1#import-the-formsmodule) – Yatharth Varshney Oct 13 '17 at 10:23
7

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

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

Sanket
  • 19,295
  • 10
  • 71
  • 82
  • 1
    thanks - it worked for me as well - although i'm using rc.5 FromModule and ngModel definition as indicated in the docs - i'm not sure why it happens. FORM_DIRECTIVES should be deprecated. – Oren Farhi Aug 25 '16 at 22:21
3

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.

wmnitin
  • 3,387
  • 1
  • 13
  • 19
2

At the app.module.ts

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

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

Yuresh Karunanayake
  • 519
  • 1
  • 4
  • 10
0

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
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
0

There's another simple substitute as follows:

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

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

enter image description here

I hope It helps you...

Neel Rathod
  • 2,013
  • 12
  • 28