question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug (?) - ReactiveForms > FormArray, trackBy index > removeAt & subscriptions

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pepemiso16commented, Apr 24, 2018

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

1reaction
robvaneckcommented, Jan 20, 2018

@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 (?)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found