我正在尝试使用表单数组提交响应式表单,我尝试过其他线程的解决方案,但都不适用,我觉得我错过了一些东西。确切的错误是ERROR错误:找不到名为"stepName"的控件和ERROR错误:找不到名称为"stepDesc"的控件。下面是一个示例代码:Html:
<div formArrayName="knowSteps" cdkDrag *ngFor="let formGroup of knowSteps.controls; let i = index">
<div [formGroup]="knowSteps.controls[i]">
<mat-accordion>
<mat-expansion-panel class="step-expand">
<mat-expansion-panel-header cdkDragHandle class="drag-drop">
<p>{{steptitle.value == '' ? 'Step' : steptitle.value}}</p>
</mat-expansion-panel-header>
<div clas="row">
<div class="col-md-12 p-3 mb-2 step-name" >
<mat-form-field appearance="outline">
<mat-label>Step Name</mat-label>
<input matInput formControlName="stepName" name="stepName" #steptitle>
</mat-form-field>
</div>
</div>
<div clas="row">
<div class="col-md-12 p-3 mb-2 step-description">
<mat-form-field appearance="outline">
<mat-label>Step Description</mat-label>
<textarea matInput formControlName="stepDesc" name="stepDesc"></textarea>
</mat-form-field>
</div>
</div>
请忽略表单中未关闭的div。
ngOnInit() {
this.knowhowForm = this.formBuilder.group({
knowSteps: this.formBuilder.array(
[[this.knowledgeStepsForm()]],
)
});
knowledgeStepsForm() {
return this.formBuilder.group({
stepName: new FormControl('', [Validators.required]),
stepDesc: new FormControl('', [Validators.required]),
})
}
get knowSteps(): FormArray {
return this.knowhowForm.get('knowSteps') as FormArray;
}
我的提交表格:
createKnowledge(){
this.newKnowledge.knowledge_steps = this.knowhowForm.get('knowSteps').value;
}
我只放置了表单的这一部分,因为这是唯一不起作用的部分。提前谢谢。
this.formBuilder.group({
stepName: new FormControl('', [Validators.required]),
stepDesc: new FormControl('', [Validators.required]),
})
应在到达CCD_ 1之前调用。通常在constructor
/ngOnInit
。