我正在使用带有TypeScript的Angular 2构建应用程序。我有一个下表,该表的其中一列包含select控件。选择控件内容绑定到另一个字符串数组-
<table ngControl="lstClasses" name="lstClasses">
<tr *ngFor="let class of model.Classes">
<td>{{class.Name}}</td>
<td>
<select name="cmbSection" [(ngModel)]="class.Section">
<option *ngFor="let section of sections"
[ngValue]="section">
{{section}}
</option>
</select>
</td>
</tr>
</table>
其中Class是类型为{Name:string,Section:string}的对象,sections是string [] = [“ White”,“ Blue”,“ Purple”,“ Green”,“ Red”]。
当我在任何行的任何组合框中更改选定的选项时,它会正确反映在模型中。当我保存并刷新数据时,所有行中的所有选择控件都将显示与表的最后一行一起保存的所选选项。
例如,如果我在表的最后一行中选择“紫色”并将其保存,则在数据刷新时,即使模型包含正确的选项,所有行也会将“紫色”显示为选定选项。
您能帮我解决这个问题吗?
我认为您正在寻找类似下面的内容,
<option *ngFor="let section of sections"
[value]="section"
[selected]="section == class.Section"
>
{{section}}
</option>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句