我遇到了与 Angular 响应式表单相关的问题,但我无法解决。
代码
form.html 和 form.ts
import {Component, OnInit} from '@angular/core';
import {FormArray, FormBuilder, FormGroup} from '@angular/forms';
import {ProcessService} from "../../../service/process.service";
@Component({
selector: 'app-check-order-form',
templateUrl: './check-order-form.component.html',
styleUrls: ['./check-order-form.component.css']
})
export class CheckOrderFormComponent implements OnInit {
submitted = false;
X: FormGroup = this._fb.group({
field: '',
Y: this._fb.array([])
});
Yg: FormGroup = this._fb.group({
subfield: '',
Z: this._fb.array([])
});
Zg: FormGroup = this._fb.group({
subsubfield: ''
});
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.createYg();
this.createZg();
}
ngOnChanges() {
}
onSubmit(formValue) {
this.submitted = true;
console.warn(this.X.value);
}
createYg() {
return this.Yg;
}
createZg() {
return this.Zg;
}
get Y(): FormArray {
return this.X.get('Y') as FormArray;
}
getCurrentZ(index): FormArray {
return this.Y.at(index).get('Z') as FormArray;
}
addY(): void {
this.Y.push(this.createYg());
}
addZ(index): void {
let Z = this.Y.at(index).get('Z') as FormArray;
Z.push(this.createZg());
}
deleteY(index) {
this.Y.removeAt(index);
}
deleteZ(Yindex, index) {
this.getcurrentZ(Yindex).removeAt(index);
}
}
<form class="form-inline" [formGroup]="X" (ngSubmit)="onSubmit(X.value)">
<div class="form-group col-3 mb-2">
<label for="field">Field</label>
<input type="text" class="form-control" formControlName="field" id="field">
</div>
<div class="form-inline" formArrayName="Y">
<div *ngFor="let y of Y?.controls; let k=index">
<hr/>
<div [formGroupName]="k" class="row pt-1 pb-1">
<div class="col-12">
<label>Y {{k + 1}}</label>
</div>
<div class="form-group col-3 mb-2">
<input type="text" class="form-control" formControlName="subfield" placeholder="subfield">
</div>
<div class="form-inline" formArrayName="Z">
<div *ngFor="let fondo of getCurrentZ(k)?.controls; let j=index">
<hr class="bg-secondary"/>
<div [formGroupName]="j" class="pt-1 pb-1">
<label>Z {{j + 1}}</label>
<div class="form-group col-3 mb-2">
<input type="text" class="form-control" formControlName="subsubfield" placeholder="subsubfield">
</div>
<div class="form-group col-3 mb-2">
<button (click)="deleteZ(k, j)" class="btn btn-danger mr-1">Remove</button>
</div>
</div>
</div>
</div>
<div class="form-group col-12 mb-2 pr-1">
<button class="btn btn-info mr-1" (click)="addZ(k)">+ Z</button>
</div>
<div class="form-group col-12 mb-2 pr-1">
<button (click)="deleteY(k)" class="btn btn-danger mr-1">
Remove
</button>
</div>
</div>
</div>
</div>
<div class="form-group col-12 mt-2">
<button type="submit" class="btn btn-primary mr-2">Submit</button>
<button (click)="addY()" class="btn btn-success">+ Y</button>
</div>
</form>
问题
以下是导致该问题的步骤(请查看上面的代码):
- 我点击“+Y”按钮以添加Y FormGroup。
- 我再次单击“+Y”。
- 我在两个Y FormGroup之一上单击“+Z”。
结果: Z FormGroup在两个Y元素上都呈现出来了。
对我而言,理想情况是每个FormGroup仅与父级相关联,以便正确编译表单。我尝试了许多解决方案,但是尽管指定了父数组(Y)的索引,我仍无法找到问题所在。
提前感谢您的帮助。