I have an issue related with Angular Reactive Form which I am not capable of sort out.
Code
form.html and 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>
Problem
Here are the steps that lead to the issue (please, have a look at the code above, please):
- I click on
+ Ybutton in order to add a Y FormGroup - I click another time on
+ Y - I click on
+ Zon one of the two Y FormGroup
Result: the Z FormGroup is rendered on both of the Y elements.
The ideal for me is that each FormGroup is related only with the parent one in order to compile the form properly. I tried many solutions, but I cannot find the issue despite of the specification of the index of the parent array (Y).
Thank you in advance.