Angular 4: setValue formBuilder empty array
EDIT:
As of newer versions, Angular now supports clearing a FormArray
with clear()
:
(<FormArray>this.form.get('memes')).clear();
ORIGINAL:
Tried a few things:reset()
,setControl()
, but the following was the only solution I found to work that actually resets the whole array to []
, other options worked, but they left the formgroups in place, just emptied the values.
So how I got it to work, was to iterate the form array and delete each form group with that particular index in the loop:
const control = <FormArray>this.form.controls['memes'];
for(let i = control.length-1; i >= 0; i--) {
control.removeAt(i)
}
If there is a better way, I'm open for suggestions! :)
Try this.myForm.controls['myFormArray'] = this.formBuilder.array([]);
with formBuilder service instantiated in your constructor.