【问题标题】:Angular Forms- FormArray: Cannot find control with path: 'strains -> i'Angular Forms-FormArray:找不到带有路径的控件:'strains -> i'
【发布时间】:2018-10-18 12:04:34
【问题描述】:

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

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])
  })

查看 (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 来执行此操作?

【问题讨论】:

  • 嗨,他们也使用 formBuilder,所以它只是可以使用 formBuilder(而不是语法 new FormArray()

标签: angular


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2017-10-28
    • 1970-01-01
    • 2021-05-20
    • 2020-08-18
    • 2020-01-05
    相关资源
    最近更新 更多