我有一个包含true / false值和一些文本的数组。当我在阵列上执行ngFor时,不会检查复选框,并且未设置输入的值。如果我只是通过{{}}显示布尔值和文本的值,我可以看到值:
<tr *ngFor="let milestone of milestones">
{{milestone.IsIncluded}} //I can see true or false
<td><input [(ngModel)]="milestone.IsIncluded" name="IsIncluded" type="checkbox"></td>
<td>{{milestone.MilestoneTitle}}</td>
<td>
{{milestone.Name}} //I can see the name
<input type="text" name="Name" [(ngModel)]="milestone.Name" class="form-control" />
</td>
</tr>
所有输入都是空白的,它没有设置来自milestone.Name的值,并且即使前两个数组的IsIncluded设置为true,也取消选中所有复选框。
答案 0 :(得分:1)
我明白了。看起来在ngFor中,我需要输入一个唯一的名称,所以我这样做了:
<tr *ngFor="let milestone of milestones;let in = index">
<td><input [(ngModel)]="milestone.IsIncluded" name="IsIncluded-{{in}}" type="checkbox"></td>
<td>{{milestone.MilestoneTitle}}</td>
<td>
<input type="text" name="Name-{{in}}" [(ngModel)]="milestone.Name" class="form-control" />
</td>
</tr>