0

In this example https://stackblitz.com/edit/angular-ivy-hptdnu?file=src%2Fapp%2Fgrid%2Fgrid.component.html without trackBy the ngOnDestroy hook is triggered and the data updated I can understand it

but in this other example https://stackblitz.com/edit/angular-ivy-aan45e?file=src%2Fapp%2Fgrid%2Fgrid.component.html with trackBy the ngOnDestroy hook is never triggered and the data is updated without any problem but I don't really understand why! I mean for me the row component if there is no destroy triggering should be not update without sort of setter on the @Input

Can you explain me, what I miss?

user3887366
  • 2,226
  • 4
  • 28
  • 41

1 Answers1

2

the answer to your question becomes obvious when you understand what trackBy is used for (e.g. https://netbasal.com/angular-2-improve-performance-with-trackby-cc147b5104e5): using trackBy you are telling angular not to destroy and re-create ngFor iterated elements, but reuse them instead. So ngOnDestroy is not triggered because components are never destroyed, but reused with new inputs.

D Pro
  • 1,756
  • 1
  • 8
  • 15
  • Thanks but the link you have posted doesn't explain how angular handle the input (its all about the items in the dom) Do you know any articles about this topic? – user3887366 Jun 19 '21 at 19:14
  • try this https://blog.angular-university.io/how-does-angular-2-change-detection-really-work/ – D Pro Jun 19 '21 at 19:41