Angular 将自定义特性设置为嵌套被动窗体结构中的所有窗体对象

Angular 将自定义特性设置为嵌套被动窗体结构中的所有窗体对象,angular,forms,nested,angular-reactive-forms,Angular,Forms,Nested,Angular Reactive Forms,我有一个复杂的角度应用程序的问题。 目前,我有一个很大的嵌套式反应式表单结构,其中有一个“主”表单组,其中包含多个FormGroup/formarray/FormControls 例如: FormGroup: { FormControl: {} FormGroup: {FormGroup: {FormControl..., FormControl...}} FormArray: [FormGroup {...}, FormGroup{...}]

我有一个复杂的角度应用程序的问题。 目前,我有一个很大的嵌套式反应式表单结构,其中有一个“主”表单组,其中包含多个
FormGroup
/
formarray
/
FormControls

例如:

 FormGroup: {  
    FormControl: {}  
    FormGroup: {FormGroup: {FormControl...,   FormControl...}}  
    FormArray: [FormGroup {...}, FormGroup{...}]    
...  
}
现在我想向大主窗体的所有子窗体添加一个自定义的
FormControl
/
属性。在我的例子中,它类似于“
只读:true

一种解决方案是将HTML中的所有子元素(表单)作为父表单提供为
@Input
,因此我必须只向父表单添加属性。 但也许有一种更为流畅的方式,我想不出


非常感谢您的建议或任何解决方案。

可以在控件上迭代FormGroup-它们是对象,但您可以使用Object.keys-您可以使用递归函数,例如

setEnabled(formGroup,value)
  {
     Object.keys(formGroup.controls).forEach(x=>{
       const gr=formGroup.get(x)
       if (gr.controls) //if has controls is a formGroup (or a FormArray)
            this.setEnabled(gr,value)
        else
        {
          console.log(gr.value) //<--show the value
          if (value)
            gr.enable()
          else
            gr.disable();
        }
     })
  }
setEnabled(表单组,值)
{
key(formGroup.controls).forEach(x=>{
const gr=formGroup.get(x)
if(gr.controls)//如果has控件是formGroup(或FormArray)
此.setEnabled(gr,值)
其他的
{
console.log(gr.value)//