Service In Angular

AngularJS service is a function or an object that is used to access the resources, like calling web service or web API, getting URL, and browsing the information and is responsible to do specific tasks only. There are 30 built-in services available in AngularJS. Example - $http, $location etc.

AngularJS services are normally injected using dependency injection (DI) mechanism of AngularJS and these can be used to organize and share the code across your app.

AngularJS services are lazily instantiated which means that a service is only instantiated when an application component depends on it and the Singletons (i.e., each component being dependent on a service) gets a reference to the single instance generated by the service factory.

Example

The following example demonstrates how to use AngularJS service. This example has used $location built-in service which provides the page URl. In this example, the btnclick function is invoked on the click of button and calls the $location service to fetch the URL string. For more details, go through the example.
  1. <HTML ng-app="myapp">    
  2.  <TITLE> AngularJS learning(Service)</TITLE>    
  3.  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>    
  4.  <script>    
  5.  var myapp = angular.module('myapp', []);    
  6.  myapp.controller('myappcont', function($scope, $location) {    
  7.  $scope.btnclick=function(){    
  8.  alert('Your URL is: '+$location.absUrl());    
  9.  }    
  10.  });    
  11.  </script>    
  12.  <BODY ng-controller="myappcont">    
  13.  <p>Click to get URL info:<button ng-click="btnclick()">Click</button></p>    
  14.  </BODY>    
  15.  </HTML>    
Creating Custom Service

Like custom filter, which we have created in my previous article, custom services can also be created. There are three ways to create a service - factory method, service method, and provider method.

Creating custom service using provider function is a bit different than the factory and service methods. We will demonstrate the use of provider method in my next article. In this write-up, we will create a custom service using factory and service method.

Creating service using Factory method

The following code example creates a service using Factory method.

  1. var myapp = angular.module('myapp', []);    
  2.  myapp.factory('FactoryMadeService', function() {    
  3.  return{    
  4.  show:function(){    
  5.    alert('This is created by factory method');    
  6.    }    
  7.    }    
  8.  });    
Creating service using Service method

Following code example creates a service using Service method.

  1. myapp.service('ServiceMadeService', function() {    
  2.  this.show=function(){    
  3.    alert('This is created by service method');    
  4.    }    
  5.  });    
Example

This example creates two 'FactoryMadeService' and 'ServiceMadeService' services. These two services display the alert messages and are called by two buttons. For more details, go through the example.

  1. <HTML ng-app="myapp">    
  2.  <TITLE> AngularJS learning(Custom Service)</TITLE>    
  3.  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>    
  4.  <script>    
  5.  var myapp = angular.module('myapp', []);    
  6.  myapp.factory('FactoryMadeService', function() {    
  7.  return{    
  8.  show:function(){    
  9.    alert('This is created by factory method');    
  10.    }    
  11.    }    
  12.  });    
  13.  myapp.service('ServiceMadeService', function() {    
  14.  this.show=function(){    
  15.    alert('This is created by service method');    
  16.    }    
  17.  });    
  18.  myapp.controller('myappcont', function($scope,FactoryMadeService,ServiceMadeService) {    
  19.  $scope.btnfclick=function(){    
  20.       FactoryMadeService.show();    
  21.  }    
  22.  $scope.btnsclick=function()    
  23.  {    
  24.  ServiceMadeService.show();    
  25.  }    
  26.  });    
  27.  </script>    
  28.  <BODY ng-controller="myappcont">    
  29.  <p>Click to call Factory Made Service:<button ng-click="btnfclick()">Click</button></p>    
  30.  <p>Click to call Service Made Service:<button ng-click="btnsclick()">Click</button></p>    
  31.  </BODY>    
  32.  </HTML>    
Summary

In this blog, we demonstrated how to create a custom service using factory and service method and discussed about the AngularJS Services. Hope this information will be helpful to you.