无法推动阵列离子

问题描述 投票:0回答:3

我是离子的新手。

我想将数据推送到我的myForm数组,但它显示错误无法读取未定义的属性'push'

这是我的表格:

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

        }),
      ]),
     })

}

我想把元素推到ref_array

这是我的代码:

    const control2 = <FormArray>this.myForm.controls['docs']
    const control3 = <FormArray>control2.controls['ref_array']

    control3.push(
      this._fb.group({
        refTextBox: []
      })
    )

我在哪里弄错了?请提前帮助和感谢!

angular ionic-framework ionic2 ionic3
3个回答
1
投票

你必须维护formArray的索引:

参考例---> DEMO

请参阅演示,它将帮助您理解表格阵列

 add(index){
    const control =  <FormArray>this.myForm.get('docs')['controls'][index].get('ref_array');
    control.push(
      this._fb.group({
        refTextBox: []
      })
    )
    }

2
投票

使用_fb.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: []
      }))

0
投票

可能control2.controls['ref_array']返回null。

添加条件

  if(control3 && control3.length >= 0){
   control3.push(
      this._fb.group({
        refTextBox: []
      })
    )
  }
© www.soinside.com 2019 - 2024. All rights reserved.