1Z1*_*Z10 2 select selected ngfor angular
我正在构建一个简单的表单,循环遍历问题列表,并针对每个问题遍历其答案列表。现在,由于我正在构建一个SELECT表单控件,通过 a 列出可用选项*ngFor,我想将第一个选项设置为默认选项,换句话说,根据局部变量的值将其选择first,但我不明白为什么以下方法不起作用。
<select [formControlName]="question.id">
<option *ngFor="let answer of answers; first as isFirst" [value]="answer.name" [selected]="isFirst">
{{answer.name}}
</option>
</select>
Run Code Online (Sandbox Code Playgroud)
建议的问题没有解决我的问题。尝试用 [attr.selected] 替换 [selected] 但没有成功。
尝试这个
<select [formControlName]="question.id">
<option *ngFor="let answer of answers; let i = index" [value]="answer.name" [selected]="i == 0">
{{answer.name}}
</option>
</select>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14768 次 |
| 最近记录: |