【发布时间】:2018-04-14 15:52:02
【问题描述】:
我刚刚开始开发我的第一个 Angular 2 应用程序,我收到以下令人困惑的错误消息:
错误:如果在表单标签中使用了 ngModel,则 name 属性 必须设置或形式 控件必须在 ngModelOptions 中定义为“独立”。
示例 1: 示例 2:
这是我得到错误的地方:
<button (click)="addRow()" class="btn">Añ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ón</th>
<th>Legislació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>
我在这一行得到错误:
<div *ngIf="numitems.invalid && (numitems.dirty || numitems.touched)" class="alert alert-danger">
但错误信息很混乱,因为我在输入字段中设置了名称:
<input name="NumItems-{{rowIndex}}" #numitems="ngModel" [(ngModel)]="productionorder.numitems" placeholder="Items por nivel" required>
此表单中的其他输入具有相同的结构,我没有收到任何错误。
这个错误来自哪里?我该如何解决?
【问题讨论】:
标签: angular