I have the following code that dynamically adds a form group containing a title and two radio buttons to create a condition, effectively providing its title and whether or not it's an acceptable condition:
export enum Acceptability {
ACCEPTABLE,
INACCEPTABLE
}
export interface Condition {
title: string;
acceptability: Acceptability;
}
export class AddCondition implements OnInit {
form: FormGroup;
ACCEPTABILITY = Acceptability;
constructor(private fb: FormBuilder) {}
ngOnInit() {
// build the form
this.form = this.fb.group({
conditions: this.fb.array([])
});
}
get conditions(): FormArray {
return this.form.get('conditions') as FormArray;
}
addCondition() {
this.conditions.push(this.fb.group({
title: ['', Validators.required],
acceptability: ['', Validators.required]
}));
}
}
<div formArrayName="conditions">
<div *ngFor="let condition of conditions.controls; let i=index" [formGroupName]="i">
<input class="form-control" formControlName="title" type="text" placeholder="title">
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="acceptable" formControlName="acceptability" name="acceptability" class="custom-control-input" [value]="ACCEPTABILITY.ACCEPTABLE">
<label class="custom-control-label" for="acceptable">Acceptable</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="inacceptable" formControlName="acceptability" name="acceptability" class="custom-control-input" [value]="ACCEPTABILITY.INACCEPTABLE">
<label class="custom-control-label" for="inacceptable">Inacceptable</label>
</div>
</div>
<button type="button" class="btn btn-outline-dark btn-block" (click)="addCondition()">
<i class="fa fa-plus fa-lg mr-3"></i>ADD A NEW CONDITION</button>
</div>
The problem is that when clicking the ADD A NEW CONDITION button, and the new form group appears, every time I select any radio button in the group array, the corresponding radio button of the first array item in the group is the one that gets selected.