为什么使用动态表单会出现此错误?
我遇到了这个错误,发现了很多相同的问题,但是其中任何一个都不是我的解决方案。
这是我的HTML:
<form [formGroup]="newPreferForm" class="example-form">
<div *ngIf="!noCriterion">
<span class="example-list-section">
<div>
<div *ngFor="let criterion of c.controls; let i=index">
<div [formGroup]="criterions">
<mat-checkbox-group formControlName="check">
<mat-checkbox *ngIf="unApprovedCriterion[i].criterionNameOfOthers"
[disabled]="existCritereion[i]">
{{unApprovedCriterion[i]. criterionNameOfOthers}}
</mat-checkbox>
<mat-checkbox *ngIf="!unApprovedCriterion[i].criterionNameOfOthers"
[disabled]=" existCritereion[i]">
{{unApprovedCriterion[i]. criterionNameOfMe}}
</mat-checkbox>
</mat-checkbox-group>
</div>
</div>
</div>
</span>
<div>
<button (click)="updateCriterions()">save</button>
</div>
</div>
</form>
类型脚本中动态表单及其控件的定义为:
export class AddCriterionComponent implements OnInit {
newPreferForm: FormGroup;
get p() { return this.newPreferForm.controls; }
get c() { return this.p.criterions as FormArray; }
}
ngOnInit(): void {
this.newPreferForm = this.formBuilder.group({
criterions: new FormArray([])
});
for (let i = this.c.length; i < this.numOfNewPreferences; i++) {
this.c.push(this.formBuilder.group({
check: []
}))
}
}