如何设置在 Angular 2 中选中的单选按钮

问题描述 投票:0回答:4

我是 Angular 2 的新手。

我有一个列表并对其进行迭代并显示为单选按钮,如下所示。 现在我想设置检查属性是否条件为真。 如何在 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>
angular
4个回答
47
投票

试试这个

<table *ngIf="optionList">  
    <tr *ngFor="let op of optionList; let i = index">
      <td>
          <input type="radio" name="optradio" [checked]=" (CONDITION HERE)"> 
          <label>{{op.option_text}} </label> 
      <td>
    </tr>
</table>

在线演示:https://embed.plnkr.co/jSht4Do3DzpoVQG2SAwl/


7
投票

你可以这样使用它

<input type="radio" name="optradio" [checked]="savedVal == currentRadioVal ? true : false" >  

3
投票

你可以用简单的方法

<input type="radio" name="Availble" [value]="true" (change)="changeLogs()" [(ngModel)]="isLogsActive" [checked]="isLogsActive">

<input type="radio" name="Unavailable" [value]="false" (change)="changeLogs()" [(ngModel)]="isLogsActive" [checked]="!isLogsActive" >

1
投票
<input type="radio" name='{{prod.configurationItem}}' value = '{{prod.additionalCost}}' [checked] = "prod.isDefault == 'Y' ? true : false">

prod 是对象项

© www.soinside.com 2019 - 2024. All rights reserved.