How to add "checked" to a group of radio buttons in a loop?

Normally checked just add at the end of input

 <input type="radio" name="item" value="value1" checked> <input type="radio" name="item" value="value2"> 

But now I am using * ngFor. I want to choose the first one.

I am trying to do this, but this does not work because checked not a class.

 <div *ngFor="#item of collection; #i = index""> <input type="radio" name="item" value="{{item}}" [ngClass]="{'checked':i === 0}"> <label>{{item}}</label> </div> 

So how can I do this? Thanks!

+6
source share
1 answer
 <input type="radio" name="item" value="{{item}}" [attr.checked]="i === 0 ? '' : null"> 
+10
source

All Articles