Sunday, 3 April 2016

Understanding AngularJS Factory, Service and Provider

In AngularJS, services are reusable singleton objects that are used to organize and share code across your app. They can be injected into controllers, filters, directives. AngularJS provides you three ways : service, factory and provider to create a service.


Factory


A factory is a simple function which allows you to add some logic before creating the object. It returns the created object.

Syntax



  1. app.factory('serviceName',function(){ return serviceObj;})

Creating service using factory method




  1. //creating module

  2. var app = angular.module('app', []);

  3.  

  4. //define a factory using factory() function

  5. app.factory('MyFactory', function () {

  6.  

  7. var serviceObj = {};

  8. serviceObj.function1 = function () {

  9. //TO DO:

  10. };

  11.  

  12. serviceObj.function2 = function () {

  13. //TO DO:

  14. };

  15.  

  16. return serviceObj;

  17. });


When to use


It is just a collection of functions like a class. Hence, it can be instantiated in different controllers when you are using it with constructor function.

Service


A service is a constructor function which creates the object using new keyword. You can add properties and functions to a service object by using this keyword. Unlike factory, it doesn’t return anything.

Syntax



  1. app.service('serviceName',function(){})

Creating service using service method




  1. //creating module

  2. var app = angular.module('app', []);

  3.  

  4. //define a service using service() function

  5. app.service('MyService', function () {

  6. this.function1 = function () {

  7. //TO DO:

  8. };

  9.  

  10. this.function2 = function () {

  11. //TO DO:

  12. };

  13. });


When to use


It is a singleton object. Use it when you need to share a single object across the application. For example, authenticated user details.

Provider


A provider is used to create a configurable service object. It returns value by using $get() function.

Syntax



  1. //creating a service

  2. app.provider('serviceName',function(){});

  3.  

  4. //configuring the service

  5. app.config(function(serviceNameProvider){});

Creating service using provider method




  1. //define a provider using provider() function

  2. app.provider('configurableService', function () {

  3. var name = '';

  4. this.setName = function (newName) {

  5. name = newName;

  6. };

  7. this.$get = function () {

  8. return name;

  9. };

  10. });

  11.  

  12. //configuring provider using config() function

  13. app.config(function (configurableService) {

  14. configurableService.setName('www.dotnet-tricks.com');

  15. });


When to use


When you need to provide module-wise configuration for your service object before making it available.

AngularJS : Factory, Service and Provider with example



  1. <html>

  2. <head>

  3. <title>AngularJS Service Factory and Providers</title>

  4. src="lib/angular.js">

  5. </head>

  6. <body>


  7. class="container" style="padding-top:20px;">


  8. ng-app="myApp" ng-controller="myController">

  9.  From Service: {{serviceName}}

  10.  From Factory: {{factoryName}}

  11.  From Provider: {{providerName}}

  12.  </div>

  13. //defining module

  14. var app = angular.module('myApp', []); 

  15. //defining service

  16. app.service('myService', function () {

  17. this.name = '';

  18. this.setName = function (newName) {

  19. this.name = newName;

  20. return this.name;

  21. };

  22. });

  23.  

  24. //defining factory

  25. app.factory('myFactory', function () {

  26. var serviceObj = {};

  27. serviceObj.name = '';

  28. serviceObj.setName = function (newName) {

  29. serviceObj.name = newName;

  30. };

  31. return serviceObj;

  32. });

  33.  

  34. //defining provider

  35. app.provider('configurable', function () {

  36. var privateName = '';

  37. this.setName = function (newName) {

  38. privateName = newName;

  39. };

  40. this.$get = function () {

  41. return {

  42. name: privateName

  43. };

  44. };

  45. });

  46.  

  47. //configuring provider

  48. app.config(function (configurableProvider) {

  49. configurableProvider.setName("Saksham Chauhan");

  50. });

  51.  

  52. //defining controller

  53. app.controller('myController', function ($scope, myService, myFactory, configurable) {

  54. $scope.serviceName = myService.setName("Saksham Chauhan");

  55.  

  56. myFactory.setName("Saksham Chauhan");

  57. $scope.factoryName = myFactory.name;

  58.  

  59. $scope.providerName = configurable.name;

  60. });


  61. </body>

  62. </html>

No comments:

Post a Comment