I am trying to use a FormArray in combination with a cdkDropList. But as soon as I add the corresponding FormGroup, the cdkDrag within that group stops working. So do any other events, like click events of buttons etc, that I removed here for simplicity.
So this works:
<div cdkDropList cdkDropListOrientation="horizontal" style="display: flex;">
<ng-container *ngFor="let group of formArray.value; let i = index" formArrayName="myFormArray">
<div cdkDrag>
<div cdkDragHandle style="padding: 1em; margin: 1em;background: red;">{{i}}</div>
</div>
</ng-container>
</div>
This doesnt:
<div cdkDropList cdkDropListOrientation="horizontal" style="display: flex;">
<ng-container *ngFor="let group of formArray.value; let i = index" formArrayName="compositeContent">
<div [formGroupName]="i" cdkDrag>
<div cdkDragHandle style="padding: 1em; margin: 1em;background: red;">{{i}}</div>
</div>
</ng-container>
</div>
neither does this:
<div cdkDropList cdkDropListOrientation="horizontal" style="display: flex;">
<ng-container *ngFor="let group of formArray.value; let i = index" formArrayName="compositeContent">
<div cdkDrag>
<div cdkDragHandle style="padding: 1em; margin: 1em;background: red;">{{i}}</div>
<div [formGroupName]="i"></div>
</div>
</ng-container>
</div>
This is the formArray getter:
get formArray(): FormArray {
return this.form.controls["formArray"] as FormArray;
}
And when adding to the FormArray from existing data from my API, this is how i create a new formArray element:
addElement(data): void {
this.formArray.insert(this.formArray.length, this.getNewElementFormGroup(data));
}
getNewElementFormGroup(data): FormGroup {
return new FormGroup({...});
}
Here is a stackblitz. Add [formGroupName]="i" to the cdkDrag div to reproduce the error.