angular2 formbuilder多嵌套formGroupName

时间:2017-05-25 16:08:48

标签: angular2-template angular2-formbuilder

我面临以下错误:

EXCEPTION: Uncaught (in promise): Error: Error in ./AccComponent class AccComponent - inline template:106:11 caused by: Cannot find control with name: 'det'

我的formbuilder如下:

this.AccForm = this.fb.group({
      accid: ['', Validators.required],
      accnbr: ['', Validators.required],
      cyc: this.fb.group({
        cycid:['', Validators.required],
        name:['', Validators.required],
        description:['', Validators.required],  
        det: this.fb.group({
          dcycid: ['', Validators.required],
          status: ['', Validators.required],
        })
      })
    });

在我的模板中,当我尝试获取formgroupname' det'我收到了错误?

    <div formGroupName="det">
           <div class="row">
             <div class="form-group>
                <span><strong>Id</strong></span>
                <input formControlName="dcycid" id="dcycid" type="number" class="form-control">
              </div>    
             <div class="form-group">
                <span><strong>status</strong></span>
                <input formControlName="status" id="status" type="text" class="form-control">
             </div>
           </div>
    </div>

备注&#39; det&#39;嵌套在3级。

知道什么是错的吗?

谢谢。

/ KOul

1 个答案:

答案 0 :(得分:5)

请找到更正后的HTML,您需要做的是按照JSON嵌套FormGroupNames

<div [formGroup]="AccForm">
 <div formGroupName="cyc">
  <div formGroupName="det">
  <div class="row">
    <div class="form-group">
      <span><strong>Id</strong></span>
      <input formControlName="dcycid" id="dcycid" type="number" class="form-control">
    </div>
    <div class="form-group">
      <span><strong>status</strong></span>
      <input formControlName="status" id="status" type="text" class="form-control">
    </div>
  </div>
</div>