1
votes

ERROR TypeError: Cannot read property 'push' of undefined

addAddress() {
    const control = <FormArray>this.AddUserstep2Form.controls['addresses'];  

    const addrCtrl = this.initAddress();

    control.push(addrCtrl);

  }
initAddress() {
    return this.fb.group({
      stateCode: [''],
      cityId: ['']
    }); 
  }
1

1 Answers

2
votes

This is happening when control is null , you need to initialize with empty array

const control = <FormArray>this.AddUserstep2Form.controls['addresses'];  
const addrCtrl = this.initAddress();
if(control && control.length > 0){
 control.push(addrCtrl);
}else
{
 control = [];
 control.push(addrCtrl);
}