You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DI feature: lifecycle hooks for services, especially OnDestroy #13618
When destroying an injector, Angular has no way AFAIK to inform the service instances in the injector that they are doomed.
If a service is holding valuable resources (e.g., a message bus holding subscriptions) it doesn't know to let go.
Expected behavior
If the service has an ngOnDestroy, Angular should call it as it destroys/releases its hold on an injector (e.g., when destroying a component).
Is this safe? Only if the service is created by the injector ... which isn't always the case ( almost anything except useClass).
OTOH, if the developer adds an ngOnDestroy method, she should expect it to be called and not put her service in compromising position.
On balance, I think its a useful feature, with no obvious substitution, and one consistent with disposable patterns in other languages.
p.s.: The only other lc-hook that makes sense is ngOnInit and the developer can easily compensate for its lack. I can't think of workaround for the lack of ngOnDestroy.
Activity
DzmitryShylovich commentedon Dec 21, 2016
#13369
wardbell commentedon Dec 21, 2016
Aha! I've been thinking about adding this for months. And now its done. Thanks for pointing it out.
angular-automatic-lock-bot commentedon Sep 10, 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.