Reputation: 137
I am new to ionic.
I want to push data to my array of myForm, but it is showing error Cannot read property 'push' of undefined
Here is my form:
public myForm: FormGroup;
constructor(){
this.myForm = this._fb.group({
docs: this._fb.array([
this._fb.group({
docName: [''],
ref_array: this._fb.array([
this._fb.group({
refTextBox: []
})
])
}),
]),
})
}
I want to push element to ref_array
here is my code:
const control2 = <FormArray>this.myForm.controls['docs']
const control3 = <FormArray>control2.controls['ref_array']
control3.push(
this._fb.group({
refTextBox: []
})
)
Where i am making mistake? Please help and thanks in advance!
Upvotes: 0
Views: 401
Reputation: 9687
use _fb.array
instead of normal Array []
refTextBox: this._fb.array([])
let data = <FormArray>this.myForm.get('docs');
let newdata = <FormArray>data.controls[0].get('ref_array');
newdata.push(this._fb.group({
refTextBox: []
}))
Upvotes: 2
Reputation: 7231
You have to maintain index for formArray:
Reference Example ---> DEMO
Please Refer Demo it will help you to understand form array
add(index){
const control = <FormArray>this.myForm.get('docs')['controls'][index].get('ref_array');
control.push(
this._fb.group({
refTextBox: []
})
)
}
Upvotes: 1
Reputation: 41397
probably control2.controls['ref_array']
returns null.
add if condition
if(control3 && control3.length >= 0){
control3.push(
this._fb.group({
refTextBox: []
})
)
}
Upvotes: 0