2
Answers

angular js service and factory

tri_inn

tri_inn

7y
243
1
1) i have seen people use many time use service to call web service to fetch data. rarely people use factory function to to call web service to fetch data......why?
 
why people prefer service than factory function ?
 
here i am posting a sample service which is calling web service.
  1. app.service("TopMenuService"function ($http) {    
  2.     this.getTopMenu = function () {    
  3.         debugger;    
  4.         return $http.get("/employee/getTopMenu");    
  5.     };    
  6. });    
  7.   
  8. app.service("LeftMenuService"function ($http) {    
  9.     this.getLeftMenu = function () {    
  10.         debugger;    
  11.         return $http.get("/employee/getLeftMenu");    
  12.     };    
  13. });    
  14.   
  15. app.controller("EmpCtrl"function ($scope, TopMenuService,LeftMenuService) {    
  16.   
  17.     GetTopMenu();    
  18.     GetLeftMenu();    
  19.   
  20.     function GetTopMenu() {    
  21.   
  22.         debugger;    
  23.         var _getTopMenu = EmployeeService.getTopMenu();    
  24.         _getTopMenu.then(function (topmenu) {    
  25.             $scope.topmenu = topmenu.data;    
  26.         }, function () {    
  27.             alert('Data not found');    
  28.         });    
  29.     }    
  30.   
  31.     function GetLeftMenu() {    
  32.   
  33.         debugger;    
  34.         var _getLeftMenu = EmployeeService.getLeftMenu();    
  35.         _getLeftMenu.then(function (leftmenu) {    
  36.             $scope.leftmenu = leftmenu.data;    
  37.         }, function () {    
  38.             alert('Data not found');    
  39.         });    
  40.     }        
  41. });   
 2) explain with example for the difference between angular service and factory function ?
 
thanks
 
Answers (2)