【发布时间】:2018-06-30 10:59:05
【问题描述】:
当组件启动或添加新行时,我正在尝试将ngFor 索引绑定到输入FormControlName。
<tr *ngFor="let item of invForm.controls.tool.controls; let i= index" [formGroupName]="i" >
<td>
<mat-form-field floatLabel='never'>
<input matInput type="number" style="text-align: right" formControlName="Id" value="{{i+1}}" >
</mat-form-field>
</td>
</tr>
我做了整个例子,请看一下id没有从输入中自动获取值。
https://stackblitz.com/edit/invoice-id
经过大量研究,我找到了trackBy: trackByFn 的简单解决方案。
<tr *ngFor="let item of invForm.controls.tool.controls; let i= index ;trackBy: trackByFn" [formGroupName]="i" >
和
trackByFn(index, item) {
item.value.Id = index+1
return index;
}
【问题讨论】: