Last active
August 29, 2015 14:05
-
-
Save HenriqueLimas/143772cca473fd8b8b57 to your computer and use it in GitHub Desktop.
Difference between Factory and Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Difference between Factory and Service | |
* | |
* Nate: All services used in AngularJs(Service, Factory) are singleton, | |
* because was created to share app information. | |
*/ | |
function obj() { | |
this.properties = 1; | |
this.properties2 = 1; | |
} | |
var myApp = angular.module('myApp', []) | |
.service('myService', obj) | |
.factory('myFactory', function() { | |
return obj; | |
}) | |
.controller('myController1', function($scope, myFactory, myService){ | |
myService.properties = 42; | |
myService.properties2 = 42; | |
$scope.myService = myService; | |
$scope.myFactory = new myFactory(); | |
$scope.myFactory.properties = 42; | |
$scope.myFactory.properties2 = 42; | |
}) | |
.controller('myController2', function($scope, myFactory, myService){ | |
console.log(myService.properties); //Console 42 | |
console.log(myService.properties2); //Console 42 | |
console.log(new myFactory().properties); //Console 1 | |
console.log(new myFactory().properties2); //Console 1 | |
$scope.myService = myService; | |
$scope.myFactory = new myFactory(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment