【发布时间】:2020-03-25 13:49:51
【问题描述】:
我有一个要在表格上显示的数组,但我需要编辑该表格一列中的所有值。我的代码是这样的:
<tr *ngFor="let line of invoice.lines; let index = index">
<td>
{{line.quantity}}
</td>
<td style="width: 35%">
{{line.productName}}
</td>
<td>
<input type="number" class="form-control" name="order_price"
id="field_order_price" [(ngModel)]="line.price"/>
</td>
...
</tr>
问题是input 在所有行中都具有相同的值(这是数组中的最后一个)。我尝试在input 旁边打印line.price 的值,以确保我得到了正确的值,这就是我得到的:
我错过了什么?
更新 1:由建议更改没有工作:(
<input type="number" class="form-control" name="order_price" (click)="onOrderPriceChange(index)" id="field_order_price1_{{index}}" [(ngModel)]="invoice.lines[index].price"/>
角度:
trackByIndex(index: number, line: IInvoiceLine): any {
return line.id;
}
【问题讨论】:
-
在
*ngFor指令中遇到[(ngModel)]的问题似乎很常见。我自己还没有遇到过这个,但也许看看这个:stackoverflow.com/a/40315703/12431728 -
我添加了
trackBy,但并没有改变问题
标签: angular