Angular 2 - Services and Dependency Injection
Let's look some definitions to understand the subject.
A Service is a functionality that can be shared with different Components. Whenever we find some common aspects in our components, we usually separate it out as a Service.
Dependency Injection is a coding pattern in which a class takes the instances of objects it needs which is called dependencies from an external source rather than creating them itself.
In Angular 2, services are Singleton. It internally implements the Service Locator Pattern. This means each service register itself under one container as a single instance. Angular provides a built in Injector which acts as a container to hold the single instances of all registered services. Angular takes care of creation of Service instance and registering it to the Service container.
2) Define the metadata with a decorator
3) Import what we need.
To understand the Service more, let's built a service in Angular 2 for getting the user profile details in the User component. Let's create some classes:
In above figure,
1. The "Service 1" is registered at app level, so it is available to all the nested components.
2. The "Service 2" is registered at "Parent 1" component, so it is available to Parent 1, Child1, Child2 and Child 3 components.
3. The "Service 3" is registered at "Child 1" component, so it is available to "Child 1" only, as no other component comes under its hierarchy.
4. The "Service 4" is registered at "Parent 2" component, so it is available to Parent 2 and Child 4 only.
Now, if we try to register "Service 1" both in root component and "Parent 1" component, then it's two instances will be created. That is why, we should register the service at an appropriate component level as per it's usage.
Continuing with our example, Import ProfileService and change the @NgModule() decorator of root level component as:
Now, we can execute the methods of our service to get the data. We can call the service method inside our constructor, but what if the service wants to pull to data from backend. We don't want the construction of our class halted till the time we pull the data.
A better solution would be to implement the OnInit interface hook provided by Angular 2 and call the service method inside ngOnInit() function. (Know more about Angular hooks here.)
Modify the Profile class as below:
* Use @Injectable() decorator to the service.
* Register the service at the root component.
* Inject the service as a constructor parameter of the dependent class.
A Service is a functionality that can be shared with different Components. Whenever we find some common aspects in our components, we usually separate it out as a Service.
Dependency Injection is a coding pattern in which a class takes the instances of objects it needs which is called dependencies from an external source rather than creating them itself.
In Angular 2, services are Singleton. It internally implements the Service Locator Pattern. This means each service register itself under one container as a single instance. Angular provides a built in Injector which acts as a container to hold the single instances of all registered services. Angular takes care of creation of Service instance and registering it to the Service container.
The @Injectable() decorator
To inject the service into component, Angular 2 provides an Injector decorator : @Injectable().We broadly have the following steps to create a Service:
1) Create the service class2) Define the metadata with a decorator
3) Import what we need.
To understand the Service more, let's built a service in Angular 2 for getting the user profile details in the User component. Let's create some classes:
1. IProfile interface
export interface IProfile{ Name: string; Age: number; }
2. Profile Class
import {Component, Injectable} from '@angular/core' import {IProfile} from './iprofile' @Component({ selector: 'profile', template: ` <div> <h2>Name : {{MyProfile.Name}}</h2> <h2>Age : {{MyProfile.Age}}</h2> </div> ` }) export class Profile{ MyProfile : IProfile; constructor(){ } }
3. ProfileService class
import {Injectable} from '@angular/core' import {IProfile} from './iprofile' @Injectable() export class ProfileService{ getProfiles(): IProfile[]{ return [{ Name:'Bob', Age: 25 }, { Name:'Alice', Age: 23 }]; } }
Registering a Service:
A Service can be registered in any component. However, it should be registered at root level component only. This is because it we register at two different nested level component, then the service will have two entire different instances in both components. However, we can register at any nested level component just in case if we require to use the service in that component or it's child components only.In above figure,
1. The "Service 1" is registered at app level, so it is available to all the nested components.
2. The "Service 2" is registered at "Parent 1" component, so it is available to Parent 1, Child1, Child2 and Child 3 components.
3. The "Service 3" is registered at "Child 1" component, so it is available to "Child 1" only, as no other component comes under its hierarchy.
4. The "Service 4" is registered at "Parent 2" component, so it is available to Parent 2 and Child 4 only.
Now, if we try to register "Service 1" both in root component and "Parent 1" component, then it's two instances will be created. That is why, we should register the service at an appropriate component level as per it's usage.
Continuing with our example, Import ProfileService and change the @NgModule() decorator of root level component as:
import {ProfileService} from './profile.service'; . . . @NgModule({ imports: [ BrowserModule ], declarations: [ App ], providers:[ProfileService], bootstrap: [ App ] })
Injecting a Service in component:
We can inject our service as a parameter to our constructor class.
constructor(private _profileService: ProfileService){ }
Now, we can execute the methods of our service to get the data. We can call the service method inside our constructor, but what if the service wants to pull to data from backend. We don't want the construction of our class halted till the time we pull the data.
A better solution would be to implement the OnInit interface hook provided by Angular 2 and call the service method inside ngOnInit() function. (Know more about Angular hooks here.)
Modify the Profile class as below:
import {Component, Injectable} from '@angular/core' import {ProfileService} from './profile.service'; import {IProfile} from './iprofile' @Component({ selector: 'profile', template: ` <div> <h2>Name : {{MyProfile.Name}}</h2> <h4>Age : {{MyProfile.Age}}</h4> </div> `, }) export class Profile implements OnInit{ ProfileList : IProfile[]; MyProfile : IProfile; constructor(private _profileService: ProfileService){ } ngOnInit(){ this.ProfileList = this._profileService.getProfiles(); this.MyProfile = this.ProfileList[0]; } }
To summarize:
* Create a Service class* Use @Injectable() decorator to the service.
* Register the service at the root component.
* Inject the service as a constructor parameter of the dependent class.
No comments:
Post a Comment
Comment Here