gpt4 book ai didi

Angular 5 *ngFor 选择 : how to set an option SELECTED if it's the first?

转载 作者:行者123 更新时间:2023-12-02 03:35:53 25 4
gpt4 key购买 nike

我正在构建一个简单的表单,循环遍历问题列表,并针对每个问题遍历其答案列表。现在,由于我正在构建一个 SELECT 表单控件,通过 *ngFor 列出可用选项,我想将第一个选项设置为默认选项,换句话说,根据局部变量 first 的值将其设为 SELECTED,但我不明白为什么以下方法不起作用。

<select [formControlName]="question.id">
<option *ngFor="let answer of answers; first as isFirst" [value]="answer.name" [selected]="isFirst">
{{answer.name}}
</option>
</select>

建议的问题没有解决我的问题。尝试将 [selected] 替换为 [attr.selected] 但没有成功。

最佳答案

试试这个

<select [formControlName]="question.id">
<option *ngFor="let answer of answers; let i = index" [value]="answer.name" [selected]="i == 0">
{{answer.name}}
</option>
</select>

working example

关于 Angular 5 *ngFor 选择 : how to set an option SELECTED if it's the first?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50230988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com