Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Life-cycle methods for services in angular2 [duplicate]

Is it possible to have life-cycle hooks for a service that is annotated with @Injectable()?

I'd have expected the life-cycle hooks to be called on a service like this, but I was proven wrong, it seems to be working on @Component only. Is there a way to get informed 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 } } 
like image 971
Martin C. Avatar asked Mar 23 '16 21:03

Martin C.


People also ask

What is lifecycle of a service in Angular?

Lifecycle hooks are a special functionality in Angular that allow us to “hook into” and run code at a specific lifecycle event of a component or directive. Angular manages components and directives for us when it creates them, updates them, or destroys them.

How many lifecycle methods does Angular offer?

There are eight lifecycle hooks in Angular: ngOnChanges() ngOnInit() ngDoCheck()

What is the correct order of lifecycle hooks in Angular?

Lifecycle sequenceInitialize the directive/component after Angular first displays the data-bound properties and sets the directive/component's input properties. Called once, after the first ngOnChanges() . Detect and act upon changes that Angular can't or won't detect on its own.

What are lifecycle hooks?

A lifecycle hook provides a specified amount of time (one hour by default) to wait for the action to complete before the instance transitions to the next state.


2 Answers

Injectables are just normal classes (normal objects) and as such, they have no special lifecycle.

When an object of your class is created, the class’s constructor is called, so that’s what your “OnInit” would be. As for the destruction, a service does not really get destroyed. The only thing that might happen is that it gets garbage collected once there is no longer a reference to it, which likely happens after the dependency injector is removed itself. But you generally have no control over it, and there is no concept of a deconstructor in JavaScript.

@Injectable() export class SampleService {     constructor() {         console.log('Sample service is created');     } } 
like image 61
poke Avatar answered Oct 25 '22 22:10

poke


The ngOn* lifecycle hooks you show are only for components. You could inject another service (call it TrackServiceLifecycles) into SampleService and have SampleService's constructor() call a method on the other service to inform it that it was created. But I can't think of a way to notify the other service when SampleService is destroyed (garbage collected).

See also ECMAScript 6 class destructor

like image 21
Mark Rajcok Avatar answered Oct 25 '22 23:10

Mark Rajcok