无法获取 FormBuilder 数组的 formControlName

Cannot get formControlName of FormBuilder Array

我在获取 FormBuilder 数组的 formControlName 时出错:

Error: Cannot find control with path: 'elements -> 0 -> name'

<form [formGroup]="targetAttributesForm" (ngSubmit)="save(myForm)">
  <input formControlName="nono" placeholder="First">

  <div formArrayName="elements">
    <div *ngFor="let address of targetAttributesForm.controls.elements.controls; let w=index" class="panel panel-default">
      <div [formGroupName]="w">
        <span>Att {{w + 1}}</span>
        --> {{ address[w] }}
        <label>Attribut name</label><input type="text" formControlName="name">
        <label>Attribut type</label>
      </div>
    </div>
  </div>
</form>

我的app.component.ts:

ngOnInit() {
    this.targetAttributesForm = this._fb.group({
       nono : ['a', Validators.required],
       elements : this._fb.array([this.initAttribut]) 
      });
}
initAttribut() {
  return this._fb.group({
        name : ['a', [Validators.required]],
        type : ['b', Validators.required]
    });
}

这是我的错误:

Error: Cannot find control with path: 'elements -> 0 -> name' Trace de la pile : _throwError@http://localhost:4200/vendor.bundle.js:69949:11 setUpControl@http://localhost:4200/vendor.bundle.js:69857:9 ../../..

我认为您打错了,因为忘记调用 initAttribut 函数:

this._fb.array([this.initAttribut()])
                               ^^^^^^
                        you need to call this function

否则 FormBuilder 将创建一个数组 FormControl 而不是一个数组 FormGroup.

StackBlitz Example