2017-09-25 99 views
1

我得到一个FormBuilder阵列的formControlName得到一个错误:无法获得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 : [email protected]http://localhost:4200/vendor.bundle.js:69949:11 [email protected]http://localhost:4200/vendor.bundle.js:69857:9 ../../..

+0

欢迎StackOverflow上。请阅读[问](https://stackoverflow.com/help/asking)和[我如何问一个好问题](https://stackoverflow.com/help/how-to-ask)。我纠正了你的问题:我简化了标题(尽可能简洁!);我使用了所有错误消息的blockquotes;我还重写了句子,删除了所有无用的部分(例如“请帮助我!”或多个问题/询问标记)。 –

+0

manu谢谢Massimiliano –

回答

0

我想你打错因为忘记打电话给你initAttribut功能:

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

否则FormBuilder将创建一个FormControl阵列,而不是一个FormGroup的阵列。

StackBlitz Example

+0

谢谢uuuu soo much,itis working –