在表格中添加 for 循环时,我在 Angular 5 中遇到错误。我试图找到原因,但他们不够清楚,我无法解决问题。我的代码和报错如下。
<tr *ngFor="let template of templates; let j=index">
<td><b>Template {{j+1}}</b></td>
<td><input class="form-control" [(ngModel)]="template"/></td>
</tr>
最佳答案
让模板创建一个局部变量,而 ngModel 不能分配给局部变量。您只能分配给组件的属性(字段)。
但你可以在组件中尝试:
public templates = ['test', 'test'];
public customTrackBy(index: number, obj: any): any {
return index;
}
模板:
<tr *ngFor="let template of templates; let j=index ; trackBy:customTrackBy">
<td><b>Template {{j+1}}</b></td>
<td><input class="form-control" [(ngModel)]="templates[j]"/></td>
</tr>
关于javascript - Angular 5 : ERROR Error: Uncaught (in promise): Error: Cannot assign to a reference or variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51611547/