Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does the constructor of an injected service in angular run?

In Angular 7, if we have a service declared as @Injectable({providedIn: 'root'}), when does it's constructor execute? Does it execute upon the construction of a component that uses it (as a dependency) or does it wait until a method in that service is first called?

Note: Since the service is a singleton, I use the service's constructor to initialize some values. I could create a "Initialize()" method and call that in the component's constructor instead but I find this aproach a little messy.

like image 581
sdagkas Avatar asked Nov 23 '18 10:11

sdagkas


People also ask

Does constructor run before ngOnInit?

OnInit : ngOnInit is component's life cycle hook which runs first after constructor(default method) when the component is being initialized. So, Your constructor will be called first and Oninit will be called later after constructor method.

How do Constructors work in Angular?

Constructor in Angular is put into use to inject dependencies into the component class. It creates a new instance of the class when the compiler calls 'new MyClass ()'. While calling 'new MyClass()', it is vital that the exact match of the parameter passes the Angular component constructor of the class.

What is injectable () in Angular service?

The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency. Likewise, the @Injectable() decorator indicates that a component, class, pipe, or NgModule has a dependency on a service. The injector is the main mechanism.

How does injection work in Angular?

Dependency injection (DI) is a paradigm. The way it works in Angular is through a hierarchy of injectors. A class receives its resources without having to create or know about them. Injectors receive instruction and instantiate a service depending on which one was requested.


1 Answers

Instances of injected services are created at the point a component that requires them is first created or when another service that requires them is first instantiated.

When a component (or another service) that requires a Service as a dependency is created (or instantiated), Angular scans the Dependency Injection tree, and looks for a definition (and an instance of the injected service). If no instance of the injected service is found, Angular creates an instance of the injected service, and only then does its constructor run.

Another way to instantiate a service before an object (or another service) that require it are created, is to add it as a dependency to the APP_INITIALIZER in the deps array, like this:

{ provide: APP_INITIALIZER, useFactory: () => () => null, deps: [MyService], multi: true }

This will create an instance of MyService when the app is initialized regardless of when other components/services that require it are created. More details here.

like image 142
Lorraine R. Avatar answered Oct 12 '22 11:10

Lorraine R.