305

Why isn't ngOnInit() called when an Injectable class is resolved?

Code

import {Injectable, OnInit} from 'angular2/core'; import { RestApiService, RestRequest } from './rest-api.service'; @Injectable() export class MovieDbService implements OnInit { constructor(private _movieDbRest: RestApiService){ window.console.log('FROM constructor()'); } ngOnInit() { window.console.log('FROM ngOnInit()'); } } 

Console Output

FROM constructor() 

5 Answers 5

451

Lifecycle hooks, like OnInit() work with Directives and Components. They do not work with other types, like a service in your case. From docs:

A Component has a lifecycle managed by Angular itself. Angular creates it, renders it, creates and renders its children, checks it when its data-bound properties change and destroy it before removing it from the DOM.

Directive and component instances have a lifecycle as Angular creates, updates, and destroys them.

Sign up to request clarification or add additional context in comments.

13 Comments

So simply move my ngOnInit logic to the constructor for Injectable classes? I just remembered reading that you should keep any logic out of the constructor for whatever reason.
@LeviFuller Yea, for services you can do initialization in the constructor, or better make init method and call it from the constructor (in case you need to reset a service later).
Not exactly, OnInit has to do with binding. You use it when you want to wait for input values to be resolved, as they are not available in the constructor. Constructor can be called multiple times. For example, when you change route and load different components they get "constructed" and destroyed each time...
It's also worth noting here that Sevices can be instantiated multiple times, depending on where you include them in providers arrays. If you want a singleton service, put it in your main module's providers, and if you want per-component services add them to the component directly.
This is not entirely incorrect, as @SBD580 points in his answer, ngOnDestroy is called for injected services
|
70

I don't know about all the lifecycle hooks, but as for destruction, ngOnDestroy actually get called on Injectable when it's provider is destroyed (for example an Injectable supplied by a component).

From the docs :

Lifecycle hook that is called when a directive, pipe or service is destroyed.

Just in case anyone is interested in destruction check this question:

1 Comment

such a shame ngOnInit isn't called :-( I really wanted to do some transparent delayed initialization magic. If ngOnDestroy can be called I don't see why init can't
13

Adding to answer by @Sasxa,

In Injectables you can use class normally that is putting initial code in constructor instead of using ngOnInit(), it works fine.

Comments

5

Note: this answer applies only to Angular components and directives, NOT services.

I had this same issue when ngOnInit (and other lifecycle hooks) were not firing for my components, and most searches led me here.

The issue is that I was using the arrow function syntax (=>) like this:

class MyComponent implements OnInit { // Bad: do not use arrow function public ngOnInit = () => { console.log("ngOnInit"); } } 

Apparently that does not work in Angular 6. Using non-arrow function syntax fixes the issue:

class MyComponent implements OnInit { public ngOnInit() { console.log("ngOnInit"); } } 

4 Comments

Injectable classes are Services, not Components. OP asks about a Service. Although your answer may be correct from a language POV, it doesn't answer this question. You should delete it.
@AndrewPhilips while my use case is slightly different from the OP's, a google search for "ngOnInit not called" leads people here. My goal with this answer is not to help the OP, but to help any of the other 70,000+ viewers who might have has similar issues with ngOnInit.
Fair enough. I believe I’ve seen SO Q&A in which an author asks and then answers their own question. As a new Angular programmer, your answer would have confused me two weeks ago. Both from an SO and NG perspective, I think your answer is worthy of its own Question, so not deleted but “recategorized”. Who knows? Perhaps it might reach an even wider audience. Cheers.
Makes sense - I edited my answer to make it clear that this is not for services.
4

I had to call a function once my dataService was initialized, instead, I called it inside the constructor, that worked for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.