Angular 2使用FormBuilder在FormArrays中访问嵌套的FormArrays

时间:2017-08-04 23:27:38

标签: angular formarray

首先,我从Angular 2开始,我尝试构建嵌套表单并验证它并将新对象项目添加到对象GroupProject中。

这是我的ts文件的一部分:

import { createSelector } from 'reselect'

const getAssets = ({ assets }) => assets; /** simple not memoized selector **/

const getAssetIds = createSelector( /** memoized selector **/
    getAssets,
    (assets) => assets.map(asset => asset.id)
);

查看:

ngOnInit() {
  this.myForm = this.formBuilder.group({
    groupproject: this.formBuilder.array([
      this.initGroupProjects()
    ])
  });
}

public initGroupProjects(): any {
    return this.formBuilder.group({
        groupproject: this.formBuilder.array([this.initProjects()])
    });
}

public initProjects(): any {
  return this.formBuilder.group({
    name: ['', [Validators.required, Validators.minLength(3)]],
    some_array: this.formBuilder.array([
      this.formBuilder.group({
        name: ['', Validators.required],
        attr: ['', Validators.required],
        some_id: [1, Validators.required]
      })
    ])
  });
}

addProject(): void {
  const control = < FormArray > this.myForm.controls['projects'];
  control.push(this.initProjects());
}

表格值的输出:

表格价值:

<form [formGroup]="myForm" novalidate (ngSubmit)="onSubmit(myForm)">
    <div formArrayName="groupproject">
        <div [formGroupName]="gp" *ngFor="let objGroupProject of myForm.controls.groupproject.controls; let gp = index">
            <div formArrayName="projects">
                <div [formGroupName]="i" *ngFor="let project of objGroupProject.controls.projects.controls; let i = index">
                    <md-input placeholder="Name" formControlName="name"></md-input>
                    <div formArrayName="some_array">
                        <div [formGroupName]="x" *ngFor="let some_obj of project.controls.some_array.controls; let x = index">
                            <div>
                                <md-input placeholder="Nome" formControlName="name"></md-input>
                                <small *ngIf="!some_obj.controls.name.valid">Nome é requerido</small>
                            </div>
                            <md-input type="number" placeholder="Cost" formControlName="attr" required></md-input>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <button type="submit" md-raised-button color="primary" [disabled]="!myForm.valid">Submit</button>
</form>
<pre>form value: <br>{{myForm.value | json}}</pre>

好吧,你可以看到我有一些名为groupprojects的数组,每个数组都有一个数组。

所以问题是我无法在对象组中插入对象项目

实际上我收到以下错误:

错误类型错误:无法读取属性&#39;控件&#39;未定义的

1 个答案:

答案 0 :(得分:1)

您很可能在以下代码中有一个类型:

public initGroupProjects(): any {
    return this.formBuilder.group({
        groupproject: this.formBuilder.array([this.initProjects()])
         ^^^^^^^
    });
}

我认为它应该是projects而不是groupproject

public initGroupProjects(): any {
    return this.formBuilder.group({
        projects: this.formBuilder.array([this.initProjects()])
    });
}

还要检查 Plunker Example

另见