【发布时间】:2021-09-20 17:51:58
【问题描述】:
我是 Angular 2 的新手。
我有一个列表并对其进行迭代并显示为单选按钮,如下所示。现在,如果条件为 TRUE,我想设置检查属性。如何在 Angular 2 中做到这一点?
<table *ngIf="optionList">
<tr *ngFor="let op of optionList; let i = index">
<td>
<input type="radio" name="optradio" *ngIf=" (CONDITION HERE) ? 'checked' : 'non' ">
<label>{{op.option_text}} </label>
<td>
</tr>
</table>
【问题讨论】:
-
你试过
op.checked作为条件吗?*ngIf=" op.checked ? 'checked' : 'non' " -
在
checked属性上使用属性绑定:[checked]="condition" -
试过 [checked] 但它总是检查最后一个单选按钮
-
上面的代码示例表明对 *ngIf 的用途存在误解(因为 OP 和其他几十个已解决)。如果有人看到这一点并且不确定,*ngIf 用于确定 DOM 元素(在本例中为 标签)是否保留在 DOM 中(如果 *ngIf 条件为真,则为是)或从 DOM 中删除.因此,根据答案,它不是要使用的东西,而且确实 [checked] 是。
标签: angular