Angular Forms- FormArray:找不到路径为“ strains-> i”的控件

时间:2018-10-18 12:04:34

标签: angular

我想用表单数组创建表单。但是我失败了

Cannot find control with path: 'strains -> i'

但是我的strainsForm包含strains

模块(TS)

  strainForm = new FormGroup({
    name: new FormControl('', Validators.required),
    daysToHarvest: new FormControl('', Validators.required)
  })
  strainsForm = new FormGroup({
    strains: new FormArray([this.strainForm])
  })

VIEW(HTML)

<form [formGroup]="strainsForm" (ngSubmit)="onSubmit()">

<section class="strains container-fluid">
  <div class="row">
    <div class="col-lg-4">
      <h3>Strains</h3>
    </div>
  </div>
  <article class="row">
    <div class="col-xs-12 col-sm-12 light-green">
      <fieldset class="col-xs-12 col-sm-12 light-green" formArrayName="strains">
        <!--<article class="strain" *ngFor="let strain of chamber.strains; let index = index" formArrayName="strains">-->
        <article class="strain" *ngFor="let strain of chamber.strains; let i = index" formGroupName="i">

我发现的所有示例都使用Formbuilder:

但是不要显示如何用formArray定义表单。还是需要FormBuilder做到这一点?

1 个答案:

答案 0 :(得分:0)

通过将formGroupName更改为[formGroupName](带括号)来解决问题