1
<li *ngFor="let x of addressdata;let i = index"> <div class="col-md-2"> <input type="radio" [value]="x.PreferedAddress" [(ngModel)]="x.PreferedAddress" (ngModelChange)="RadioCheck(i)" name="radiogroup" id="Prefered"/> </div> </li> 

from db i get 3 rows and binding correctly, for particular row only have PreferedAddress=1 remaining has value 0. but in list by default last row radio button is checked

2
  • what you exactly want? can you please share your angular code? Commented May 15, 2018 at 6:29
  • Radio button workes well with the name. name must be the same for one group of radio buttons. Commented Jan 10, 2021 at 12:30

5 Answers 5

1

There many ways to bind radio button using *ngFor:

<li *ngFor="let x of addressdata;let i = index"> <div class="col-md-2"> <input type="radio" [checked]="x.PreferedAddress==1" (ngModelChange)="RadioCheck(i)" name="radiogroup" /> </div> </li> 

This is way to do it based on your requirement

Note: If you are using [(ngModel)] then you don't need id and value attribute.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Sanoj , Its working fine . may i know what is the issue if i fill [(ngModel)]
You can check the documentation here (angular.io/api/forms/NgModel)
model value not getting if i remove [(ngModel)] this. if i select another radio button .. how can i get that change
You can change it from this RadioCheck(i) function find index and update value to 1 if you select current radio button and rest of update value set to 0.
Remove (ngModelChange)="RadioCheck(i)" instead `(change)="RadioCheck(i)" like this.
1

you use name property dynamic and check in your array variable assign for radio button must have type Boolean.improve code below..

in html use name="radiogroup-{{i}}"

Comments

0

You use primitive values (strings), so you need to use a custom trackby function.

customTB(index, item) { return `${index}-${item.PreferedAddress}`; } 

In your HTML

<li *ngFor="let x of addressdata;let i = index; trackBy: customTB"> <div class="col-md-2"> <input type="radio" [value]="x.PreferedAddress" [(ngModel)]="x.PreferedAddress" (ngModelChange)="RadioCheck(i)" name="radiogroup" id="Prefered"/> </div> </li> 

Comments

0

checked needs to get string, anyway, this is my solution:

getChekedState(x: any): string { if (x.PreferedAddress==1) { return 'checked'; } else { return ''; } } 

then in your html:

[checked]="getChekedState(x);" 

Comments

-1

the problem is that you didn't set your id and code value here is your improve code

 <li *ngFor="let x of addressdata;let i = index"> <div class="col-md-2"> <input type="radio [value]="x.PreferedAddress [(ngModel)]="x.PreferedAddress" (ngModelChange)="RadioCheck(i)" name="radiogroup" id="Prefered-{{i}}" /> </div> </li> 

maybe this code help you and if its not work then share your complete code with me so I can help you

1 Comment

why someone gives me -1? this code helps you to set an individual to radio button value and it's work for me?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.