In the referenceI read:
Lastly, it is important to realize that all Angular services are application singletons. This means that there is only one instance of a given service per injector.
but with this simple code seems not to be a singleton
'use strict'; angular.module('animal', []) .factory('Animal',function(){ return function(vocalization){ return { vocalization:vocalization, vocalize : function () { console.log('vocalize: ' + this.vocalization); } } } }); angular.module('app', ['animal']) .factory('Dog', function (Animal) { return Animal('bark bark!'); }) .factory('Cat', function (Animal) { return Animal('meeeooooow'); }) .controller('MainCtrl',function($scope,Cat,Dog){ $scope.cat = Cat; $scope.dog = Dog; console.log($scope.cat); console.log($scope.dog); //$scope.cat = Cat; });
I'm a little confused can you explain me what's the matter ?
UPDATE 1 May be I'm not the sharpest tool in the shed but afer the @Khanh TO reply it would be a better explanation in the reference it's not very clear.
UPDATE 2
'use strict'; angular.module('animal', []) .factory('Animal',function(){ return { vocalization:'', vocalize : function () { console.log('vocalize: ' + this.vocalization); } } }); angular.module('dog', ['animal']) .factory('Dog', function (Animal) { Animal.vocalization = 'bark bark!'; Animal.color = 'red'; return Animal; }); angular.module('cat', ['animal']) .factory('Cat', function (Animal) { Animal.vocalization = 'meowwww'; Animal.color = 'white'; return Animal; }); angular.module('app', ['dog','cat']) .controller('MainCtrl',function($scope,Cat,Dog){ $scope.cat = Cat; $scope.dog = Dog; console.log($scope.cat); console.log($scope.dog); //$scope.cat = Cat; });
BOOM it's a singleton !
UPDATE 3
But if you do like
'use strict'; angular.module('animal', []) .factory('Animal',function(){ return function(vocalization){ return { vocalization:vocalization, vocalize : function () { console.log('vocalize: ' + this.vocalization); } } } }); angular.module('app', ['animal']) .factory('Dog', function (Animal) { function ngDog(){ this.prop = 'my prop 1'; this.myMethod = function(){ console.log('test 1'); } } return angular.extend(Animal('bark bark!'), new ngDog()); }) .factory('Cat', function (Animal) { function ngCat(){ this.prop = 'my prop 2'; this.myMethod = function(){ console.log('test 2'); } } return angular.extend(Animal('meooow'), new ngCat()); }) .controller('MainCtrl',function($scope,Cat,Dog){ $scope.cat = Cat; $scope.dog = Dog; console.log($scope.cat); console.log($scope.dog); //$scope.cat = Cat; });
it works
AngularJS services are: Lazily instantiated – AngularJS only instantiates a service when an application component depends on it. Singletons – Each component dependent on a service gets a reference to the single instance generated by the service factory.
The main objective of angular services is to share data across Angular application. Practically an angular service can be shared between all the components or can be limited to some component. Hence Angular service can be a singleton as well as non-singleton in nature.
Services are singleton objects which are instantiated only once in app and are used to do the defined task.
In AngularJS, a service is a function, or object, that is available for, and limited to, your AngularJS application. AngularJS has about 30 built-in services. One of them is the $location service.
It's singleton, there is only one object, but is injected into many places. (objects are passed by reference to a method)
All your Animal
are object pointers referring to the same animal object which is a function in your case. Your Cat
and Dog
are objects constructed by this function.
Yes, service is singleton. The following code log only one "M" to console:
function M() { console.log('M'); } function M1(m) { console.log(1); } function M2(m) { console.log(2); } angular.module('app', []) .service('M', M) .service('M1', ['M', M1]) .service('M2', ['M', M2]) .controller('MainCtrl',function(M1, M2){});
run it in jsbin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With