I'm creating a dynamic FormArray within a FormGroup. The only change I've made is switching from *ngFor to the new @for. The issue occurs when I try to remove an element from the FormArray.
Previously, I simply used the remove function, and it would remove exactly the element where I clicked. However, with the new @for, the behavior is different. Even though the correct index or element is removed from the FormArray, in the DOM, it visually removes the last element of the list instead.
For example, if I have five elements and delete the second one, the FormArray correctly removes that index. But in the UI, the last element disappears instead, making it seem like the last option is being deleted rather than the intended one.
@for (opcion of listaOpciones.controls; track $index; let a = $index){
<div class="row mb-2 mb-md-0 card_opciones" [formGroupName]="a" >
<div class="titulo_mobile mb-2 etiqueta col-12"></div>
<div class="col-1 contenedor_quitar">
<button class="col-2 btn boton_quitar" (click)="deleteOption(a)" type="button">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
}
My function to delete is this.
deteleteOption(index: any) {
this.listaOpciones.removeAt(index);}