DEV Community

Discussion on: When a service got destroyed in angular

Collapse
 
hp13055 profile image
F.1.armchair.expert • Edited

@Bo
Your conclusion is wrong.
ngOnDestroy is fired BEFORE the directive gets destroyed. As you can read above, its supposed to be used to clean up resources. Nonetheless the directive may not get destroyed for some reason (for example because a reference is holding up to it). But still : it's working as intended => the event is fired because the component is supposed(!) to fade away.

Collapse
 
bo profile image
Bo Vandersteene

My articles is about services so providers and not directives. So the conclusion is for using ngOnDestroy on a service and NOT on a directive

Thread Thread
 
hp13055 profile image
F.1.armchair.expert • Edited

I've read the article again. You open it with
"If we read the OnDestroy lifecycle hook api description Angular OnDestroy we can use it on services."
But, how do you came to that conclusion? The lifecycle is described for components and directives. Where do you read it could be used on sercices?
You may have misunderstood whats written for onDestroy(). Services are mentioned there, right, but its meant as example to use onDestroy on your component to unregister it from(!) services.