Bug (?) - ReactiveForms > FormArray, trackBy index > removeAt & subscriptions
See original GitHub issueHi 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?
Issue Analytics
- State:
- Created 6 years ago
- Comments:16 (1 by maintainers)
Top Results From Across the Web
Controls of FormArray not displayed correctly after removing a ...
I have a component with a list of inputs in a FormArray. ... let i=index; trackBy: trackByIndex" [formGroupName]="i"> <input type="text" ...
Read more >Angular Reactive Forms - Dynamic Form Fields using FormArray
In this post, we are going to look at how to add and remove form controls dynamically in Angular Reactive Forms using FormArray....
Read more >Demo: Removing Items from a FormArray - Thinkster.io
Now we'll learn how to remove them. First, we'll add a new method called removePhoneNumber(), which takes a parameter of index so that...
Read more >Angular Formarray Doesn't Delete Item When Using Removeat
I have a large Reactive Form with a FormArray. and I trackBy index. So since i can't rely on index to remove a...
Read more >And Possibly Last - Look At Reactive Forms In Angular 7.2.13
Ben Nadel looks at Reactive Forms for the first time in Angular ... The formGroupName uses the index of the FormArray iteration, essentially....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is 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 (?)