I have a formControl (Radio) in parent formGroup and wanted to use it within formArray. Usecase is MCQ Question is having multiple options (formArray), wanted to have Radio button with each option to choose correct answer (Only One)
JS
private createForm() {
this.form = new FormGroup({
question: new FormControl('', Validators.required),
correctOption: new FormControl(false),
options: new FormArray([this.newOption(), this.newOption()])
});
}
private newOption(): FormGroup {
const validators = [Validators.required];
return new FormGroup({
value: new FormControl('', validators),
explanation: new FormControl('')
});
}
HTML
<div [formGroup]="form">
<div formArrayName="options">
<div *ngFor="let option of form.get('options')['controls']; let idx = index">
<div class="form-group" [formGroupName]="idx">
<div class="input-group" style="align-items: center">
<textarea class="form-control" id="text" placeholder="" formControlName="value" autosize
id="option_{{idx}}"></textarea>
<span class="input-group-append">
<button class="btn btn-primary" type="button" (click)="removeOption(idx)" tabindex="-1"><i
class="fas fa-trash" aria-hidden="true"></i></button>
</span>
<span class="input-group-append ml-2 fas">
<input id="answer_option_{{idx}}" name="correctOption" type="radio" value="{{idx}}"
formControlName="<How to use parent form control correctOption?>" />
</span>
</div>
<div>
<ckeditor [editor]="editor" formControlName="explanation"
[config]="{ placeholder: 'Enter explanation' }">
</ckeditor>
</div>
</div>
</div>
</div>
</div>

root level formcontrolname?