Angular2嵌套formGroups - formArrays和模板绑定

时间:2017-04-20 17:25:02

标签: javascript html angular typescript angular2-forms

问题在于:

我有一个带有嵌套表单组的复杂表单,这里是一个"简化"结构如下:

  <form [formGroup]="myForm">
    <ion-list radio-group formControlName="listOptions">
      <ion-item>
        <ion-label>Option 1</ion-label>
        <ion-radio value="1" (ionSelect)="myFunction($event)"></ion-radio>
      </ion-item>
    </ion-list>
  </form>

角度方面,它应该看起来像这样(不正确/完整,但只是给出一个想法):

-> MyForm (formGroup)
    -> Whatever01 (formControl - input)
    -> Whatever02 (formControl - input)
    -> WhateverGroup01 (formGroup)
        -> Whatever03 (formControl - input)
        -> Whatever04 (formControl - input)
    -> WhateverArray01 (formArray)
        -> WhateverGroup02 (formGroup)
            -> Whatever05 (formControl - input)
            -> WhateverGroup03 (formGroup)
                -> Whatever06 (formControl - input)
        -> WhateverGroup04 (formGroup)
            -> Whatever07 (formControl - input)
    -> ...

从root myForm项目到获取formArray的事情变得非常精细,但随后出现了问题....

所以,我无法访问&#34;无论05和#34; (和06但05不起作用......所以......)formControl将它绑定到模板!这是模板实际看起来的样子(在数组之前有意地跳过了部分),有趣的部分是[????] =&#34; ????&#34;,实际上这是问题。

this.myForm = this.fb.group({
    whatever01: ['',Validators.compose([...])],
    whatever02: ['',Validators.compose([...])],
    whateverGroup01: this._formBuilder.group({
        whatever03: ['',Validators.compose([...])],
        whatever04: ['',Validators.compose([...])]
    }),
    whateverArray01: this._formBuilder.array([
        this._formBuilder.group({
            whatever05: ['',Validators.compose([...])],
            whateverGroup03: this._formBuilder.group({
                whatever06: ['',Validators.compose([...])]
            })
        }),
        this._formBuilder.group({
            whatever07: ['',Validators.compose([...])],
        })
    ])
    ...
});

formArray中的formGroups具有完全相同的结构。

基本上,问题是我无法访问formGroups中的formControl ...

我尝试了几个解决方案,我使用[formGroupName],formGroupName(没有括号),[formGroup],formControlName,...但是无法弄清楚我必须使用什么才能将相应的formGroup / formControl绑定到数据!

我现在遇到这种错误:

<form [formGroup]="myForm" ...>
    <!-- ...skipped... -->
    <div formArrayName="whateverArray01" *ngFor="let item of myForm.controls.whateverGroup01.controls.whateverArray01.controls;let idx = index;" [formGroupName]="idx">
        <div [????]="????">
            <input formControlName="whatever05" ... />
            <div [????]="????">
                <input formControlName="whatever06" ... />
            </div>
        </div>    
    </div>
</form>

感谢您阅读/帮助

这是一个带有一些代码的plunker:

http://plnkr.co/edit/wf7fcHMkhIwzmLWLnCF8?p=preview

2 个答案:

答案 0 :(得分:2)

表单的构建对我来说似乎是正确的。您在模板中遇到一些问题,FormArray的迭代不正确,FormArray不在FormGroup WhateverGroup01内,因此迭代应如下所示:< / p>

*ngFor="let ctrl of myForm.controls.whateverArray01.controls;

关于FormArray的模板部分,它应如下所示:

<div *ngFor="let ctrl of myForm.controls.whateverArray01.controls; let i = index" [formGroupName]="i">
   <input formControlName="whatever05" />
   <div formGroupName="whateverGroup03">
      <input formControlName="whatever06" />
   </div>
</div>

那应该解决问题:)

这是一个

Demo

答案 1 :(得分:2)

首先,您的视图绑定错误:

[formGroupName]="whateverGroup02"

组件中没有whateverGroup02属性。我们应该按如下方式传递字符串:

formGroupName="whateverGroup02"

然后,如果你建立组如:

this.fb.group({
  x: 1,
  y: {
    z: 2
  }
})

你会得到

FormGroup
  -> FormControl with name `x` and value `1`   
  -> FormControl with name `y` and value `{ z: 2 }`

这意味着如果你想操纵嵌套的formGroups,你必须创建它们。 即。

let fGroups = data.map(rs => this.buildFormGroup(rs));
...
buildFormGroup(obj) {
  let formGroup: { [id: string]: AbstractControl; } = {};
  Object.keys(obj).forEach(key => {
    formGroup[key] = typeof obj[key] === 'object' ?
      this.buildFormGroup(obj[key]) : 
      new FormControl(obj[key]);
  });
  return this._formBuilder.group(formGroup);
}

<强> Plunker Example