Injector, Injectable And Inject In Angular
The Angular Injector is the brains behind the Angular Dependency Injection architecture. In this article we will discuss in more details. The @Injectable and @Inject decorators will also be examined.
What is Angular Injector
The Angular Injector is in charge of instantiating and injecting dependencies into components and services.
Using the Injection token, the Injector searches the Angular Providers for the dependency. The Provider, which holds information on how to construct an instance of the dependent, is returned by the Angular Providers array. The Injector is responsible for creating the instance and injecting it into the Component or Service.
When is Angular Injector is created
When the application bootstraps, Angular generates two injector trees. The ElementInjector tree is for Elements, while the ModuleInjector tree is for Modules (Components & Directives etc).
When the application bootstraps, Angular loads the Root Module (AppModule). For the Root Module, it makes RootModule Injector. The application-wide reach of this injector is impressive. The ModuleInjector Tree gains the RootModule Injector as a child.
The AppComponent, the root component of our project, is loaded by Angular Root Module. The AppComponent is provided with its own injector. Injector is the name we give to this root. The root of the ElementInjector tree becomes this Injector.
All additional components are contained in the Root Component. Under the Root Component, Angular App will build child components. All of these kid components can have their own children, forming a component tree. In addition, Angular constructs an Injector for each of those components, resulting in an Injector tree that roughly resembles the component tree. The ElementInjector tree now contains these Injectors.
Registering the service with injector
All of the application's dependencies are registered with the Providers. A Provider is associated with each injector. We register our reliance in the Providers metadata of @NgModule, @Component, or @Directive.
providers: [ProductService, LoggerService]
@Injectable
Example of Injectable
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core'; @Injectable() export class LoggerService { log(message:any) { console.log(message); } }
ProductService
import { Injectable } from '@angular/core'; import {Product} from './Product' import {LoggerService} from './logger.service' @Injectable() export class ProductService{ constructor(private loggerService: LoggerService) { this.loggerService.log("Product Service Constructed"); } public getProducts() { this.loggerService.log("getProducts called"); let products:Product[]; products=[ new Product(1,'Memory Card',500), new Product(1,'Pen Drive',750), new Product(1,'Power Bank',100) ] this.loggerService.log(products); return products; } }
@Injectable({ providedIn: 'root' }) export class SomeService{ }
@Inject
export class ProductService{ constructor(@Inject(LoggerService) private loggerService) { this.loggerService.log("Product Service Constructed"); } }