Reputation: 89
I have a formGroup , one of the formControls is an array.
this.newPurchase = fb.group({
supplierName: ["", Validators.required],
supplierNumber: ["", Validators.required],
supplierEmail: ['', Validators.required],
stockitems: [[], Validators.required],
});
As you can see , stockitems is declared as an array and I would like to push an object to it.
This is what I tried:
exampleObject = {
name:'foo',
lastName:'fooAgain'
}
this.newPurchase.controls.stockitems.push(this.exampleObject)
The object did not get pushed into the array , any suggestions ?
Upvotes: 0
Views: 3642
Reputation: 519
this.newPurchase = fb.group({
supplierName: ["", Validators.required],
supplierNumber: ["", Validators.required],
supplierEmail: ['', Validators.required],
stockitems: this.fb.array([], Validators.required),
});
in your component, put this function:
get stockItemFormControls(): FormArray {
return <FormArray>this.newPurchase.get('stockitems');
}
Then where needed:
this.stockItemFormControls.push(this.fb.group({name: 'foo', lastName:'fooAgain'}));
or
this.stockItemFormControls.push(this.fb.group(this.exampleObject));
Upvotes: 3