如何将对象数组转换为表单组数组?

时间:2017-11-16 09:39:54

标签: javascript angular

我想使用formbuilder的数组函数来处理formcontrol数组。我有一个现有的自定义对象数组需要添加到FormArray但是它只需要FormGroup作为参数。如何将我的自定义对象数组转换为FormGroup对象,以便将它们添加到FormArray中?我已经尝试过了:

let formGroupArray: FormGroup[] = [];
    for(let i=0; i < account.laborperiods.length; i++){
      let formGroup = this.fb.group({
        id: this.account.laborperiods[i].id,
        beginDate: this.account.laborperiods[i].beginDate,
        endDate: this.account.laborperiods[i].endDate,
        hours: this.account.laborperiods[i].hours,
        account: this.account.laborperiods[i].account
      })
        formGroupArray.push(formGroup)
    }
    this.laborPeriodArray = new FormArray(formGroupArray);

但是这给了我错误"ERROR TypeError: Circular reference in value argument not supported"

那么有人能告诉我如何将自定义对象数组转换为FormGroup数组吗?或者也许有一种更方便的方法可以将自定义对象数组直接转换为FormArray?

1 个答案:

答案 0 :(得分:0)

尝试:

let formGroupArray:FormArray
for(let i=0; i < account.laborperiods.length; i++){
      let formGroup = this.fb.group({
          ...
      })
      if (!formGroupArray)
        formGroupArray=new FormArray(formGroup);
      else
        formGroupArray.push(formGroup)
}
//Change this line
this.laborPeriodArray = formGroupArray;