Skip to content

DI feature: lifecycle hooks for services, especially OnDestroy #13618

Closed

Description

@wardbell

I'm submitting a ... (check one with "x")

[ ] bug report 
[x] feature request
[ ] support request 

Current behavior

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.

All

  • Angular version: 2.3.0
  • Browser: [all ]
  • Language: [all ]

Activity

  1. DzmitryShylovich commented on Dec 21, 2016

    @DzmitryShylovich
    Contributor
  2. wardbell commented on Dec 21, 2016

    @wardbell
    ContributorAuthor

    Aha! I've been thinking about adding this for months. And now its done. Thanks for pointing it out.

  3. angular-automatic-lock-bot commented on Sep 10, 2019

    @angular-automatic-lock-bot

    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.

  4. locked and limited conversation to collaborators on Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions