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.

ngOnDestroy is not triggered

See original GitHub issue

Steps 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:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
adammartin1981commented, Jun 2, 2016

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:

var children = this.contentChildren;
for (var i = 0; i < children.length; i++) {
   children[i]._destroyRecurse();
}
children = this.viewChildren;
for (var i = 0; i < children.length; i++) {
   children[i]._destroyRecurse();
}

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:

var children = this.contentChildren;
var i = children.length;

while( i > 0 ) {
   children[i - 1]._destroyRecurse();
   i--;
}

I might knock up a PR at some point, but I didn’t know what anyone else thought?

0reactions
angular-automatic-lock-bot[bot]commented, Sep 8, 2019

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.

Read more comments on GitHub >

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

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