【发布时间】:2018-08-14 04:18:54
【问题描述】:
在 angular 2 代码中使用 ngFor 填充选项的下拉菜单。我需要默认选择任何给定索引处的任何特定选项。
我使用过 [attr.selected]="i == 0" ,但它选择最后一个选项而不是第一个。
这是我的代码
<select id="roles" required formControlName="userRole">
<option *ngFor="let roles of URoles;let i = index" [attr.selected]="(i==0)" [value]="roles._id">{{ roles.name }}</option>
</select>
可能是什么问题?为什么没有选择第一个选项?
【问题讨论】:
-
为什么在这种情况下使用 ng-container?只需将 *ngFor 添加到选项元素
-
也许这个答案会有所帮助stackoverflow.com/questions/37663158/…
标签: angular typescript angular2-template