Dennis
Dennis

Reputation: 87

setValue of an empty FormGroup

I have a method, that returns a FormGroup object.

public static createFormGroup(...): FormGroup { ... returns formGroupWithControls}

Generally, I already have a FormGroup within a FormArray. I want to create new FormGroups and replace the value of the FormGroup (in the FormArray) with the value of the new created FormGroup (from the above method)

this.removeControls(); // removes all Controls in "formGroupReferencedInFormArray"
this.formGroupReferencedInFormArray.patchValue(FormGroupFactory.createFormGroup(...));

After this call the FormGroup "formGroupReferencedInFormArray" is empty... But I would like to have the value of the returned FormGroup within "formGroupReferencedInFormArray".

Upvotes: 0

Views: 789

Answers (1)

Andrei Gătej
Andrei Gătej

Reputation: 11979

You could use formArray.setControl(index, youFormGroupInstance).

Source code

Upvotes: 1

Related Questions