“控制”类型上不存在属性“推送”

时间:2016-09-02 13:48:25

标签: arrays angular typescript

我正在尝试创建一个包含数组的数组。

该数组用于动态表单,因此当用户想要添加一个新部分时,它将控制字段数组推送到数组,然后在Angular2中生成它。

但是我收到此错误: Error:(43, 41) TS2339: Property 'push' does not exist on type 'AbstractControl'.

这是我的代码:

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)
    }));
}

我的构造函数,初始化表单

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([])
        })
    }

我已经按照Angular2 issue 5871的建议尝试了(<Control>this.addForm.controls['design']).push,但这还没有解决它。

我正在使用TypeScript和Angular2 beta.17

1 个答案:

答案 0 :(得分:2)

尝试转换为ControlArray(应该声明推送)而不是转换为控件。

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