有没有办法合并两个表单数组控件?

Nat*_*gic 1 angular angular-reactive-forms

我的代码中有两个表单数组。我需要组合它们并对组合数组执行添加和删除。我需要连接每个表单数组的表单数组控件

let a = this.nextBillForm.controls["electricityBillCycleEnergyCharges"] as FormArray;
let b = this.nextBillForm.controls["electricityBillCycleOtherCharges"] as FormArray;
Run Code Online (Sandbox Code Playgroud)

1)为每个填充

a.forEach(element => {
  b.push(element);
});
Run Code Online (Sandbox Code Playgroud)

2) 串联

 a.concat(b)
Run Code Online (Sandbox Code Playgroud)

两种方式都试过了。都显示错误

Lud*_*vik 6

FormArray不是数组。它没有也forEach没有concat功能。不过,您可以对controls财产进行操作:

a.controls.forEach(control => {
 b.push(control);
});
Run Code Online (Sandbox Code Playgroud)

或者

const combined = a.controls.concat(b.controls);
Run Code Online (Sandbox Code Playgroud)

请参阅FormArray 文档