If I try to set a FormArray value when the FormArray is empty:
const array1 = new FormArray([]);
array1.setValue(['']);
I get this error:
There are no form controls registered with this array yet. If you're using ngModel, you may want to check next tick
If I add 2 when there is 1:
const array2 = new FormArray([new FormControl('')]);
array2.setValue(['', '']);
I get the following error:
Cannot find form control at index 1
If I try to set 1 when there are 2:
const array3 = new FormArray([new FormControl(''), new FormControl('')]);
array3.setValue(['']);
I get the following error:
Must supply a value for form control at index: 1.
Question: How do I write a function which can update the value of a Form Array with a list of arbitrary length?
StackBlitz: https://stackblitz.com/edit/angular-wduzv9?file=src/app/app.component.ts
*edit
I understand that I can use the push(), at(), and removeAt(0) methods. I'm looking for a general-purpose function. It can use any methods you want.