If ngModel is used within a form tag set name or s

2020-05-30 07:39发布

问题:

I've just started developing my first Angular 2 app and I ge the following confuse error message:

Error: 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}">

This is where I get the error:

<button (click)="addRow()" class="btn">A&ntilde;adir</button>
<form #productionOrderForm="ngForm" (ngSubmit)="onSubmit()">
    <table class='table' *ngIf="productionorders?.length > 0">
        <thead>
            <tr>
                <th>Nombre</th>
                <th>Num. items primer nivel</th>
                <th>Reducci&oacute;n</th>
                <th>Legislaci&oacute;n</th>
                <th>Producto</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="let productionorder of productionorders; let rowIndex = index">
                <td>
                    <input name="Name-{{rowIndex}}" #name="ngModel" [(ngModel)]="productionorder.name" placeholder="Nombre" required>
                    <div *ngIf="name.invalid && (name.dirty || name.touched)" class="alert alert-danger">
                        <div *ngIf="name.errors.required">
                            Obligatorio.
                        </div>
                    </div>
                </td>
                <td>
                    <input name="NumItems-{{rowIndex}}" #numitems="ngModel" [(ngModel)]="productionorder.numitems" placeholder="Items por nivel" required>
                    <div *ngIf="numitems.invalid && (numitems.dirty || numitems.touched)" class="alert alert-danger">
                        <div *ngIf="numitems.errors.required">
                            Obligatorio.
                        </div>
                    </div>
                </td>
                <td>
                    <law (notifyParent)="getNotification($event)"></law>
                </td>
                <td>
                    <select [(ngModel)]="productionorder.productid" #productId="ngModel">
                        <option></option>
                        <option *ngFor="let product of products" [value]="law.lawId">{{law.name}}</option>
                    </select>
                </td>
            </tr>
        </tbody>
    </table>

    <button *ngIf="productionorders?.length > 0 && law != ''" type="submit" class="btn btn-success" [disabled]="disableSubmit()">Guardar cambios</button>
</form>

I get the error at this line:

<div *ngIf="numitems.invalid && (numitems.dirty || numitems.touched)" class="alert alert-danger">

But the error message is confuse because I have set the name in the input field:

<input name="NumItems-{{rowIndex}}" #numitems="ngModel" [(ngModel)]="productionorder.numitems" placeholder="Items por nivel" required>

The others input in this form have the same structure and I don't get any error in them.

Where is the error? How can I fix it?

回答1:

I have found where the error is. I have put it here to help someone that has the same error and the same knowledge about Angular (or programming).

The error is in the following select, it hasn't a name. I did this to fix it:

<select name="ProductId-{{rowIndex}}" #productId="ngModel" [(ngModel)]="productionorder.productid" required>
    <option></option>
    <option *ngFor="let product of products" [value]="law.lawId">{{law.name}}</option>
</select>


回答2:

Error: If ngModel is used within a form tag set name or set standalone error

Simple Soultions:

Add name attribute to input field in case of ngform or ngsubmit you are using

Example 1: <input [(ngModel)]="User.age" name="age">



or 

**if you are using direct [(ngModel)]="User.age" ,,,then add this [ngModelOptions]="{standalone: true}" to input field**

Example 2: <input [(ngModel)]="User.age" [ngModelOptions]="{standalone: true}">


回答3:

Each input element has a name property that is required by Angular forms to register the control with the form. This is applicable to template-driven forms.

foo.html

 <div class="form-group">
<label for="power">Hero Power</label>
<select class="form-control" id="power" [(ngModel)]="model.power" name="power" 
 required>
  <option *ngFor="let pow of powers"  [value]="pow">{{pow}}</option>
 </select>
 </div>

In Hero.model.ts

export class Hero{

constructor(
    public id:number,
    public name:string,
    public power:string,
    public alterEgo?:string
 ){}
 }

name property should be same in .html file. In foo.ts file

   model = new Hero(13, 'Dr IQ', this.powers[1], 'abcd');


回答4:

This can also happen if you have added ngModel to a button, by mistake.

<button type="submit" class="btn btn-primary" ngModel >Submit</button>

Just remove ngModel from button to fix this error.



回答5:

Try this format

<input type="text" class="form-control" name="name" placeholder="Name"
                  required minlength="4" #name="ngModel"
                  ngModel>
<div *ngIf="name.errors && (name.dirty || name.touched)">
    <div [hidden]="!name.errors.required" class="alert alert-danger form-alert">
        Please enter a name.
    </div>
    <div [hidden]="!name.errors.minlength" class="alert alert-danger form-alert">
        Enter name greater than 4 characters.
    </div>
</div>


回答6:

when ngModel is used within a form tag set name or set standalone error I did this to fix it:

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


回答7:

As Vans mentioned above, if your select element does not have a name attribute, you will get the following error :

Error: 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.


回答8:

Remove form tag in your HTML file then it automatically resolves your issue



标签: angular