Andrew Howard
Andrew Howard

Reputation: 3092

Can't patch a blank form array

I have a form:

this.myForm = this.fBuilder.group({
            speeches: this.fBuilder.array([])
});

And then later I want to just patch up the entire array of speeches with an outside array coming from another component. Example data:

newData = [{id: "wavesurfer_mt87bwp5cyi", start: 0.8766667991737204, duration: 2.006666969971786, characterId: 0},
{id: "wavesurfer_glxyt1be2js", start: 4.0033339384320845, duration: 2.2600003415961307, characterId: 1}]

And I want to patch it like so:

this.myForm.controls.speeches.patchValue(newData);

However Angular doesn't amend the blank array to my intended array of objects, how come? Must I always have each property as a proper form control? I don't need it to be as these values will never be used in an actual form field.

Upvotes: 0

Views: 553

Answers (1)

Sam
Sam

Reputation: 1851

If you simply want to store the array of values against a formControl "speeches" you don't need to use a FormArray for this.

this.myForm = this.fBuilder.group({
     speeches: [[]]
});

this.myForm.controls.speeches.patchValue(newData);

Upvotes: 1

Related Questions