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

wal*_*wal 18 angular

我是Angular 2的新手.

我有一个列表并迭代它并显示为单选按钮,如下所示.现在我想在条件为TRUE时设置check属性.如何在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>
Run Code Online (Sandbox Code Playgroud)

Tie*_*han 36

试试这个

<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>
Run Code Online (Sandbox Code Playgroud)

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

  • 它应该是一个布尔值而不是`[checked] ="(CONDITION HERE)?true:false"` (4认同)

Dan*_*sih 7

你可以这样使用它

<input type="radio" name="optradio" [checked]="savedVal == currentRadioVal ? true : false" >  
Run Code Online (Sandbox Code Playgroud)