I'm trying to insert FormGroups into a FormArray before a component is loaded. This is my FormGroup:
/*constructor (private fb: FormBuilder) {}*/
this.productGroup = this.fb.group({
name: ['', Validators.compose([Validators.required, Validators.maxLength(80)])],
variants: this.fb.array([
this.fb.group({
_id: '',
type: '',
options: ''
})
]),
});
And this is how I'm inserting a FormGroup inside variants FormArray:
const variants = <FormArray>this.productGroup.controls.variants;
variants.push(this.fb.group({ _id: '', type: '', options: '' }));
Problem is, variants.length value can be 3, 4 and so on. How to deal with it?
// variants.lenght == 2
variants.push(this.fb.group({ _id: '', type: '', options: '' }));
variants.push(this.fb.group({ _id: '', type: '', options: '' }));
// variants.lenght == 3
variants.push(this.fb.group({ _id: '', type: '', options: '' }));
variants.push(this.fb.group({ _id: '', type: '', options: '' }));
variants.push(this.fb.group({ _id: '', type: '', options: '' }));
variantslength is variable