简体   繁体   中英

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?

the answer to your question becomes obvious when you understand what trackBy is used for (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM