如何以反应形式获取嵌套组控件

How to get nested group controls in reactive forms

我在从主 formgroup.

检索嵌套 formgroup 时遇到错误

这里是错误:

Type 'AbstractControl[]' is not assignable to type '{ [key: string]: AbstractControl; }'.
  Index signature for type 'string' is missing in type 'AbstractControl[]'

我的代码:

 get form(): { [key: string]: AbstractControl } {
    return this.patientForm.controls;//works
  }

  formChild(group: string): { [key: string]: AbstractControl } {
    const childControls = (this.patientForm.get(group) as FormArray).controls;
    return childControls; //throws above error. 
  }

有人帮我解决这个问题吗?使用 get 运算符,是否可以从主窗体组中获取子组? 提前致谢。

FormArray 包含 AbstractControls.

的数组

如果您想获得 AbstractControls 的键映射,您应该期望从 getter:

返回 FormGroup
formChild(group: string): { [key: string]: AbstractControl } {
  const childControls = (this.patientForm.get(group) as FormGroup).controls;
                                                       ^^^^^^^^^^
  return childControls; //throws above error. 
}