【发布时间】:2021-04-25 12:56:24
【问题描述】:
我正在根据数组的长度迭代单选按钮列表,并希望将选中的设置为仅第一个元素/输入字段的默认值
我怎样才能将一个字段设置为选中而其他字段不选中?
我正在使用 *ngFor 来迭代项目 用于双向数据绑定的 ngModel
<div class="pt-3 flex flex-nowrap">
<div *ngFor="let color of colors" class="mr-2">
<input id="{{color}}" type="radio" [(ngModel)]="getColor" value="{{color}}" [defaultChecked]="getChecked()" name="radio" class="hidden" />
<label class="mr-2 items-center flex border-2 border-white" for="{{color}}">
<div class="h-5 w-5 bg-{{color}}-500 m-0.5"></div>
</label>
</div>
</div>
这是我的组件
export class AppComponent {
colors = ['red', 'green', 'blue'];
getColor = 'gray';
private isChecked = false;
getChecked(): boolean {
if (!this.isChecked) {
this.isChecked = true;
return this.isChecked;
}
}
}
【问题讨论】:
-
[checked]="indexOfElement == 0" 但仍然无法正常工作..
标签: angular angular-forms