ngOnDestroy is not triggered
See original GitHub issueSteps to reproduce and a minimal demo of the problem demo http://plnkr.co/edit/7uoVecfa62i8No8GtQHI?p=preview Angular2 beta 16 or higher - When I hide first section with nested components using *ngIf, ngOnDestroy of every nested component is triggered - this is OK.
<div *ngIf="!ff2">
<my-component
></my-component>
<my-component
></my-component>
<my-component
></my-component>
<my-component
></my-component>
<my-component
></my-component>
</div>
Outpu in console is
init
init
init
init
init
destroy
destroy
destroy
destroy
destroy
But when I hide second section where subcomponents are duplicated by *ngFor, not every ngOnDestroy is triggered.
<div *ngIf="!ff">
<my-component
*ngFor="#i of [1,2,3,4,5,6]"
></my-component>
</div>
output in console is
(6) init
(3) destroy
Current behavior not every ngOnDestroy is triggered
Expected/desired behavior every ngOnDestroy should be triggered
Other information in angular2 beta 9 it was working correctly http://plnkr.co/edit/Q8tLJKlpF6wEVcMWfxH1?p=preview
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:5 (1 by maintainers)
Top Results From Across the Web
ngOnDestroy not firing on page reload - Stack Overflow
ngOnDestroy only fires when the component is destroyed inside the angular workflow. However, refreshing the page is outside of the workflow ...
Read more >ngOnDestroy is not triggering · Issue #10066 - GitHub
Good morning fellows I've developed a small angular2-client-app which got routes. The routes are created like this: app.routes.ts import ...
Read more >Where ngOnDestroy Fails You - Dev Genius
ngOnDestroy doesn't get called because some components do not get destroyed when navigating to a different route.
Read more >NgOnDestroy doesn't get called?. #ng_gotchas 1 | ng-gotchas
When the time came to have a service that needs to get destroyed when a component is destroyed I simply slapped “extends OnDestroy”...
Read more >A Deep Dive into Angular's NgOnDestroy - Wesley Grimes
Many developers are surprised to learn that ngOnDestroy is only fired when the class which it has been implemented on is destroyed within...
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
Think I’ve found the issue, it was to do with iterating over a moving target (a decreasing array).
Inside core/src/linker/view.ts, there is a piece of code at line 203:
Where children always decreased, but i remained a constant, you could never hit the even items in the array, as i would be ‘1’ on the second loop, but the [1]th item in the array would now be the 3rd item (as the original [0]th item in the array would have been removed and the new [0]th item in the array would now be the 2nd).
A quick fix would be to have:
I might knock up a PR at some point, but I didn’t know what anyone else thought?
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.