Is it possible to have lifecycle hooks for a service that is annotated with @Injectable() ?
I would expect lifecycle hooks to be called for such a service, but I turned out to be wrong, it seems to work only on @Component . Is there a way to get information in a service when dependency injection creates / destroys a service?
import {Component, Injectable, OnInit, OnDestroy} from 'angular2/core'; @Injectable() export class SampleService implements OnInit, OnDestroy { ngOnInit() { console.log("OnInit") } ngOnDestroy() { console.log("OnDestroy") } } @Component({ selector: "sample", template: "<div>Sample Component</div>", providers: [ SampleService ] }) export class SampleComponent { constructor() { private _sampleService: SampleService } }
dependency-injection angular
Martin C. Mar 23 '16 at 21:24 2016-03-23 ββ21:24
source share