angular: Bug (?) - ReactiveForms > FormArray, trackBy index > removeAt & subscriptions
Hi Angular,
I think i found a [x] bug, but I’m not sure.
I have a large Reactive Form, with a FormArray… and I trackBy index. Each FormGroup in the array has subscriptions like:
// get the rides so we can iterate over them
get myRides(): FormArray {
return this.ridesForm.get('rides') as FormArray;
};
trackByFn(index, item) {
return index; // or item.id
}
// each ride that is added has subscriptions
subscribeChanges(ride: FormGroup){
ride['dateSub$'] = ride.get('date').valueChanges.subscribe(val => {
});
ride['distanceSub$'] = ride.get('distance').valueChanges.subscribe(val => {
});
ride['startCountSub$'] = ride.get('start_count').valueChanges.subscribe(val => {
});
ride['endCountSub$'] = ride.get('end_count').valueChanges.subscribe(val => {
});
}
now… you can also delete FormGroup’s using:
// deletes a single ride
public deleteRide(ride: FormGroup, i: number): void{
this.unsubscribeChanges(ride);
this.myRides.removeAt(i)
}
unsubscribeChanges(ride: FormGroup){
ride['dateSub$'].unsubscribe();
ride['distanceSub$'].unsubscribe();
ride['startCountSub$'].unsubscribe();
ride['endCountSub$'].unsubscribe();
}
but it seems that the tracking of objects goes wrong… So let’s delete a FormGroup at index 3 of 6 FormGroups…The subscriptions on the index of your removed FormGroup - and below that - do not fire anymore. As if their binding is still on a ‘old’ index.
Is it not allowed to put your Subs$ on a FormGroup? Or is there a bug in the tracking / refreshing of FormGroup’s in the array?
About this issue
- Original URL
- State: open
- Created 9 years ago
- Comments: 25 (1 by maintainers)
That’s similar to doing
which solves this issue.
6 year later and the problem persists. Angular v18 same thing with new control flow syntax.
instead of
use
hey do we have any workaround?
Edit---- instead of using index for tracking use the object instead that solves my problem
instead of fusion of fusions.controls; track i; let i = $index;;
use this fusion of fusions.controls; track fusion; let i = $index;
I know it will create a performance issues but there is no other way to do it without this
Hi,
We faced a similar issue with formArray items. After understanding how trackBy works, I found that it needs to be provided with a unique ID to allow Angular’s ngFor to correctly identify and remap elements from the DOM after the structure changes, such as when an item is removed from the array or reordered through drag-and-drop functionality.
Forked and updated the stakblitz provide above in the comment and see it is working there…
https://stackblitz.com/edit/angular-mhaf99?file=app%2Fapp.component.ts,app%2Fmock.ts
Still got the issue with angular v17. And, as
trackis mandatory with the new control flow syntax, this is quite annoying…We are noticing the same behavior as @samherrmann with angular v13
FormArray.removeAt()always removes the last element no matter what index is passed.I am experiencing this issue as well using Angular v13. Here is my minimalistic reproduction which only involves a
trackByfunction andFormArray.removeAt(): https://stackblitz.com/edit/angular-ivy-bqwaqd?file=src/app/app.component.htmlIs there a way we delete specific FormArray withou changing the initial index of active FormArray? For example, formGoupname were 0,1,2,3 when we delete 2 it should be 0,1,3 instead of 0,1,2
@jotatoledo https://stackblitz.com/edit/angular-l4v8pr
reproduction steps are inside demo. Don’t know if it is my own stupidity? I had expected track trackby index would give Angular all it needed to track the objects and manage the subscriptions properly (?)