Jake Groves Jake Groves - 3 months ago 23
TypeScript Question

Property 'push' does not exist on type 'Control'

I am trying to create an array that has arrays within it.

The array is for a dynamic form, so when the user wants to add a new section it push's the array of control fields to the array, and it generates it in Angular2.

However I am getting this error:

Error:(43, 41) TS2339: Property 'push' does not exist on type 'AbstractControl'.


this is my code:

addDesign(){
this.addForm.controls['design'].push(this.fb.group({
name: this.fb.control(null, Validators.required),
a1: this.fb.control(null, Validators.required),
a2: this.fb.control(null, Validators.required),
a3: this.fb.control(null, Validators.required),
a4: this.fb.control(null, Validators.required),
a5: this.fb.control(null, Validators.required),
maxMark: this.fb.control(null, Validators.required)
}));
}


My Constructor, initialising the form

constructor(private fb: FormBuilder, private auth: Auth, private authHttp: AuthHttp) {
this.addForm = fb.group({
name: fb.control(null, Validators.required),
assignID: fb.control(null, Validators.required),
design: new ControlArray([])
})
}


I have tried doing
(<Control>this.addForm.controls['design']).push
as suggested in Angular2 issue 5871, but this has not solved it.

I am using TypeScript and Angular2 beta.17

Answer

Rather than casting to control, try casting to ControlArray (which should have push declared).

(<ControlArray>this.addForm.controls['design'])