- Home
- Forum
- Angular - EJ 2
- RadioButton doesn't work as expected in FormArray
RadioButton doesn't work as expected in FormArray
Hello,
Inside a fromArray, a have multiple formGroup with 2 radioButton formControls.
When I check one of them, in a FormGroup, the value of the radioButton is changed (checked) from one formGroup to another, no matter what the order is.
I want that this radioButton to change independently, from one FormGroup to another.
The rest of the input works as expected.
Please, offer some support, thanks.
Template
I tried to change the name by appending the index, but this doesn't work :)
FormArray
Hi Dana,
Sorry for the delay. We have prepared the sample based on your requirement and reason for reported issue is rendering the radio button with same name/formControlName for every added form group. To overcome this issue, we need to assign the unique name/formControlName for every radio button group shown as below.
[app.component.html]:
|
<form [formGroup]="myForm" (ngSubmit)="onSubmit()"> <div formArrayName="items"> <div *ngFor="let item of items.controls; let i = index"> <div [formGroupName]="i"> <ejs-radiobutton formControlName="personType{{i}}" label="Persoana fizica" name="personType{{i}}" (change)="personTypeRadioButtonSelected($event)"> </ejs-radiobutton> <ejs-radiobutton formControlName="personType{{i}}" label="Persoana fizica" name="personType{{i}}" (change)="personTypeRadioButtonSelected($event)"> </ejs-radiobutton> </div> </div> </div> |
[app.component.ts]:
|
addItem(): void { const newItem = this.formBuilder.group({ ['personType' + this.i]: [null, [Validators.required]] }); this.items.push(newItem); this.i++; } |
Sample link: https://stackblitz.com/edit/angular-uqqwjk?file=src%2Fapp.component.html
Get back to us if you need any further assistance on this.
Regards,
YuvanShankar A
- 1 Reply
- 2 Participants
-
DS Dana Sarghe
- Jun 30, 2023 04:13 PM UTC
- Jul 6, 2023 11:48 AM UTC