-2

I am getting this error when I use push() on the array in an Angular 10 project in Typescript.

Error

Property 'push' does not exist on type '() => void'.ts(2339)

Please help me figure out why this error is coming and how can I resolve it?

export class GrantProgramComponent implements OnInit {
  grantProgramForms : any = this.fb.array([]); 
  constructor(private fb: FormBuilder ) { }

  ngOnInit(): void {
    this.AddGrantProgramForms(); 
  }


  AddGrantProgramForms(){
    this.AddGrantProgramForms.push(this.fb.group({
      Id : [0], 
      ProgramName : [''], 
      ProgramCode : [''], 
      StartDate : [''],
      EndDate : [''], 
      Status : [false]
    })); 
  }

}
StarLord
  • 707
  • 1
  • 8
  • 21

1 Answers1

1

You can't use the formbuilder before it's been initialised in the constructor. Move your logic into the constructor method:

 grantProgramForms : any 

 constructor(private fb: FormBuilder ) {
   this.grantProgramForms = this.fb.array([]); 
 }

Then in your addGrantProgramForms method you are referencing the wrong variable, I think you mean this.grantProgramForms.push not this.AddGrantProgramForms.push

chrismclarke
  • 1,995
  • 10
  • 16