Angular2 If ngModel is used within a form tag, either the name attribute must be set or the form

Tampa picture Tampa · Sep 4, 2016 · Viewed 232.9k times · Source

I am getting this error from Angular 2

core.umd.js:5995 EXCEPTION: Uncaught (in promise): Error: Error in app/model_exposure_currencies/model_exposure_currencies.component.html:57:18 caused by: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.

Example 1:

<input [(ngModel)]="person.firstName" name="first">

Example 2:

<input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}"> 
<td *ngFor="let lag of ce.lags">
    <div class="form-group1">
        <input name="name" [(ngModel)]="lag.name" [ngModelOptions]="{standalone: true}"  class="form-control" pattern="[0-9]*(\.[0-9]+)?" required>
    </div>
</td>

This is how I use form tag:

<form #f="ngForm" (ngSubmit)="onSubmit()">

Answer

Thyagarajan C picture Thyagarajan C · Nov 7, 2016

If ngForm is used, all the input fields which has [(ngModel)]="" must have an attribute name with a value.

<input [(ngModel)]="firstname" name="something">