Understand Services In Angular 2

This article helps you to understand Services in Angular 2.

Introduction
 
Services in Angular 2 are JavaScript functions, which are responsible for performing a single task. Services may have their associated properties and the methods, which can be included in the component. Services are injected, using DI (Dependency Injection).
Angular 2 has only one way to create a Service as compared to Angular 1.x. There are five ways to create a Service: Service, Factory, Constant, Provider and Values. Also, there is no clear documentation about where to use which. Following three steps are required to create an Angular 2 Service.
  • Import the injectable member.
  • Add the @Injectable Decorator.
  • Export Service class.
The basic syntax is given below to define Services in Angular 2.
  1. import { Injectable } from '@angular/core';  
  2.   
  3. @Injectable()  
  4. export class MyService {  
  5.       
  6. }  
Service example

In the example given below, I have created a function named GetText, which returns simple text "Text from Service".
  1. import { Injectable } from '@angular/core';  
  2.   
  3. @Injectable()  
  4. export class MyService {  
  5.     GetText() {  
  6.         return "Text From Service";  
  7.     }  
  8. }  
The Service in Angular 2 is a JavaScript function, which can be used by one or more components. We can either import the Service within the component or in module class, so that multiple components can access the Service. We will learn both the scenario.
 
Importing the Service to Component
 
There are four simple steps to use/import Service in the component.
  • Import the Service to the component.
  • Add it as a provider.
  • Include it through Dependency Injection.
  • Use the Service function. 
 
 
app.component.ts
  1. import { Component } from '@angular/core';  
  2. import { MyService } from './app.service';  
  3.   
  4. @Component({  
  5.   selector: 'test-app',  
  6.   templateUrl: './app/example.html',  
  7.    providers: [MyService]  
  8. })  
  9.   
  10. export class AppComponent {   
  11.     name = "Jignesh";  
  12.     messageText = '';    
  13.     constructor(private _myService: MyService) {  
  14.   
  15.     }  
  16.     onClickMe() {    
  17.         this.messageText = this._myService.GetText();    
  18.     }  
  19. }  
Example.html 
  1. <div>  
  2.     <h4>Event Binding</h4>  
  3.     <br/>   
  4.     <button (click)="onClickMe()">Click me!</button>    
  5.     <br/><br/>    
  6.     <span> {{messageText}} </span>  
  7. </div>   
 
 
Include the Service in module
 
In this case, we need to add Service as the provider property in to module instead of the component. This step is different when we include Service into the specific component. The Services can be included either in feature module or root module, which depends on the requirement.
 
In the example given below, I have included Service in root module.
 
App.module.ts
  1. import { NgModule }      from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { FormsModule } from '@angular/forms';  
  4. import { MyService } from './app.service';  
  5.   
  6.   
  7. import { AppComponent }  from './app.component';  
  8.   
  9. @NgModule({  
  10.   imports:      [ BrowserModule, FormsModule],  
  11.   declarations: [ AppComponent],      
  12.   bootstrap:    [ AppComponent ],  
  13.   providers: [MyService]  
  14. })  
  15.   
  16. export class AppModule {   
  17. }  
Now, all the components within the module are able to access the Service. There is no longer need to include the Service in the providers array within the component's metadata.
 
Inject Service into other Service
 
We can also inject one Service into other Service. Process of injecting one Service to other Service is same as injecting the Service into the component.
 
Now, I am creating OtherService in this project and injecting this Service in to MyService.
 
OtherService.ts
  1. import { Injectable } from '@angular/core';  
  2.   
  3. @Injectable()  
  4. export class OtherService {  
  5.     GetMyText() {  
  6.         return "Text From Other Service";  
  7.     }  
  8. }    
MyService.ts
  1. import { Injectable } from '@angular/core';    
  2. import { OtherService } from './OtherService';     
  3.     
  4. @Injectable()    
  5. export class MyService {    
  6.     constructor(private otherService:OtherService) {    
  7.         
  8.     }    
  9.     
  10.     GetTextFromOtherService() {    
  11.         return this.otherService.GetMyText();    
  12.     }    
  13. }  
Summary
 
Services in Angular 2 are JavaScript functions, which are responsible for performing a single task. Services may have their associated properties and methods, which can be included in the component or other Service.