0

Angular code

public radioForm: FormGroup = this.formBuilder.group({
        selectedMainAnswer: this.formBuilder.array([], [Validators.required])
    });


const control = <FormArray>this.radioForm.controls['selectedMainAnswer'];
        for (const s of this.selectionControl) {
            const selectedForm: FormGroup = this.formBuilder.group({
                label: [s.label, [Validators.required]],
                key: [s.key, [Validators.required]]
            });
            control.push(selectedForm);
        }

Html

<span
          class="g-radio-wrapper"
          formArrayName="selectedMainAnswer"
          *ngFor="let selection of radioForm.get('selectedMainAnswer')['controls']; let i = index"
        >
          <div [formGroupName]="i">
            <input
              type="radio"
              value="selection.value.key"
              id="radio-{{ i }}-{{ index }}"
              name="radio-type-{{ index }}"
              class="g_input-radio--block"
            />
            <label for="radio-{{ i }}-{{ index }}" class="g_label-radio g_label-radio--horizontal">{{
              selection.value.label
            }}</label>
          </div>
    </span>

How to get user selected radio button value, I have tried below code

this.claimRadioForm.get('selectedMainAnswer') as FormArray;

But above I am getting all info instead only the selected one.

Prashobh
  • 9,216
  • 15
  • 61
  • 91
  • 1
    `FormArray` is not suitable for holding radio button selection. You should consider another way. See [this post](https://stackoverflow.com/questions/58353034/how-to-get-selected-radio-button-values-from-mat-radio-group-with-for-loop-in-an). – N.F. Nov 25 '20 at 09:09
  • ok thanks, I should use (change) then, I was looking some form array method which can give all selected :( – Prashobh Nov 25 '20 at 09:51

0 Answers0