Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set radio button checked in Angular 2

Tags:

I am new to Angular 2.

I have a list and iterate it and display as radio buttons as shown below. Now I want to set the check property if condition is TRUE. How to do that in 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> 
like image 233
wal Avatar asked Feb 06 '17 02:02

wal


2 Answers

try this

<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> 

Online demo: https://embed.plnkr.co/jSht4Do3DzpoVQG2SAwl/

like image 53
Tiep Phan Avatar answered Sep 30 '22 16:09

Tiep Phan


You can use it like this

<input type="radio" name="optradio" [checked]="savedVal == currentRadioVal ? true : false" >   
like image 20
Daniel Masih Avatar answered Sep 30 '22 16:09

Daniel Masih