FormGroupDirective中的Form Array(多个组件中的多个表单)



我有一个很大的表单,所以我把它分解到3个组件中,并使用诸如1.tab1打开组件-12.表2打开部件-23.表3打开组件-3

我在父组件中创建表单,并使用类似的formGroup指令在子组件中使用该表单

<ng-container [formGroup]="Form">
<Component-1
formGroupName="form1"
*ngIf="OpenTab == 'Component1'"
></Component-1>
<Component-2
formGroupName="form2"
*ngIf="OpenTab == 'Component2'"
></Component-2>
<Component-3
formGroupName="form3"
*ngIf="OpenTab == 'Component3'"
></Component-3>
</ng-container>

在我的组件2中,我有一个表单和一个表,当用户填写表单时,它会显示在表中,并且用户可以填写多个任务,所以他们在一个表中有多行。在组件3中,我必须发送api中的所有3个表单(表单2作为数组(。我知道我的组件2中需要formArray,但我不知道如何实现它。我已经完成了组件1和组件2,但我无法弄清楚如何使用组件2。

我从您的问题中了解到,您已经将表单分解为子表单组件。

您可以将formArray嵌套在formGroup中,如下所示:

this.form = this.formBuilder.group({
myArray: this.fb.array([]),
// other fields
})

最新更新