Search code examples
angularangularjs-track-by

Angular polling trackBy doesn't trigger ngDestroy but the component is updated


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?


Solution

  • 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.