AngularJS Factory & Services

Diff bwn factory and services

by manoj_antony32

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js"></script>
<div ng-controller="testCtrl">
  {{name}}
  <ul>
    <li ng-repeat="user in users">{{user}}</li>
  </ul>
</div>

JavaScript

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

function InboxService($http) {
	return {
    getUsers : function() {
      return $http.get('https://reqres.in/api/users/2');
    }  
  }

}

app.factory('getUserDts', InboxService);

app.controller('testCtrl', ['$scope','getUserDts', function ($scope, getUserDts) {

	$scope.name = "mano";
  $scope.users = '';
  
  getUserDts.getUsers().then(function(data){
   $scope.users = data.data.data;
  })

console.log("https://toddmotto.com/factory-versus-service");

}]);



/* function InboxService($http) {
  this.getUsers = function getUsers() {
    return $http.get('https://reqres.in/api/users/2');
  };
}
app.service('getUserDts', InboxService); */