【发布时间】:2020-10-02 17:51:06
【问题描述】:
我正在构建一个嵌套的动态表单,其中用户有一个组,然后可以在该组中嵌套条件,或者在group FormArray 中嵌套新的其他组对象。这是基本 UI 的样子。请注意,并非所有部分都有效,但现在我正在尝试添加一个嵌套组。嵌套组适用于 FormBuilder,但它会给出错误并且不会在 UI 上正确显示。错误是:ERROR Error: Cannot find control with path: 'statement -> groups -> 0 -> groups -> conditions'。在继续之前,可以找到 StackBlitz HERE
表单对象如下所示:
{
"statement": {
"groups": [
{
"conjunctor": null,
"conditions": [
{
"variable": ""
}
],
"groups": []
}
]
}
}
在statement → groups → groups 中,用户可以推送一个额外的FormGroup,其中包含一个“组”对象:
{
"conjunctor": null,
"conditions": [
{
"variable": ""
}
],
"groups": []
}
从长远来看,我希望能够推送其他组并进一步嵌套此表单,但现在,我正试图让它在 UI 上工作。 HTML 如下所示和in this StackBlitz。我继续收到错误:
ERROR Error: Cannot find control with path: 'statement -> groups -> 0 -> groups -> conditions' ,并基于几个 S.O.例如,我认识到此错误是由于我的 HTML 嵌套方式以及 FormGroups 和 FormArrays 造成的,其中一定存在问题。但是,我似乎无法让它工作以嵌套和显示嵌套组。
以下是我尝试过的一些方法:
Angular FormArray: Cannot find control with path
Angular: Cannot find control with path: 'variable-> 0 -> id'
Angular 7 and form arrays error of cannot find a control with path
ERROR Error: Cannot find control with path
作为旁注,我不确定这是否是实现嵌套可重用组件的最佳方法,但我希望在我停止出错后进一步研究这一点。
<form [formGroup]="form">
<div formArrayName="statement">
<div formArrayName="groups">
<div *ngFor="let group of form.get('statement.groups')['controls']; let i = index">
<fieldset>
<legend>Group {{ i + 1 }}:</legend>
<div [formGroupName]="i">
<span style="float: right;">
<button type="button" style="float: right; cursor: pointer; margin-left: 5px;" (click)="deleteGroup(i)">
delete group
</button>
<button type="button" style="cursor: pointer; margin-left: 5px;" (click)="addNestedGroup(i)">
add nested group
</button>
<button
type="button"
style="cursor: pointer; margin-left: 5px;"
(click)="addNewCondition(group.controls.conditions)"
>
add condition
</button>
</span>
<div formArrayName="conditions">
<div *ngFor="let condition of group.get('conditions')['controls']; let j = index">
<fieldset>
<legend>Condition {{ j + 1 }}</legend>
<div [formGroupName]="j">
<input style="vertical-align: middle;" type="text" formControlName="variable" />
<button
style="float: right; margin-bottom: 5px;"
(click)="deleteCondition(group.controls.conditions, j)"
>
delete condition
</button>
</div>
</fieldset>
</div>
</div>
<ng-container>
<div formArrayName="groups">
<div *ngFor="let num of group.get('groups').value; let idx = index">
<fieldset>
<legend>Group {{ 2 }}:</legend>
<span style="float: right;">
<button
type="button"
style="float: right; cursor: pointer; margin-left: 5px;"
(click)="deleteGroup(0)"
>
delete group
</button>
<button type="button" style="cursor: pointer; margin-left: 5px;" (click)="addNestedGroup(0)">
add nested group
</button>
<button
type="button"
style="cursor: pointer; margin-left: 5px;"
(click)="addNewCondition(num.conditions)"
>
add condition
</button>
</span>
<div formArrayName="conditions">
<div *ngFor="cond; of: group.controls; let k = index">
<fieldset>
<legend>Condition {{ k + 1 }}</legend>
<div [formGroupName]="k">
<input style="vertical-align: middle;" type="text" formControlName="variable" />
<button
style="float: right; margin-bottom: 5px;"
(click)="deleteCondition(group.controls.conditions, k)"
>
delete condition
</button>
</div>
</fieldset>
</div>
</div>
</fieldset>
</div>
</div>
</ng-container>
</div>
</fieldset>
</div>
</div>
</div>
</form>
【问题讨论】:
-
您想要有多少个嵌套级别?我可以解决您当前的问题,但我认为您会要求提供更复杂的示例
-
最终我希望有 3-4 层深度(与现在显示的 2 层相比):组 → 组。从长远来看,我想象有组→组→组→组。尽可能深入地嵌套
标签: javascript html angular