我想用表单数组创建表单。但是我失败了
Cannot find control with path: 'strains -> i'
但是我的strainsForm
包含strains
。
strainForm = new FormGroup({
name: new FormControl('', Validators.required),
daysToHarvest: new FormControl('', Validators.required)
})
strainsForm = new FormGroup({
strains: new FormArray([this.strainForm])
})
<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做到这一点?
答案 0 :(得分:0)
通过将formGroupName
更改为[formGroupName]
(带括号)来解决问题