Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Angularjs services singleton?

Tags:

angularjs

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

like image 282
Whisher Avatar asked Feb 01 '14 09:02

Whisher


People also ask

What is singleton pattern AngularJS?

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.

Why services in angular are singleton?

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.

Are services singleton objects which are instantiated only once in angular app?

Services are singleton objects which are instantiated only once in app and are used to do the defined task.

What are services in AngularJS?

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.


2 Answers

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.

like image 74
Khanh TO Avatar answered Oct 14 '22 23:10

Khanh TO


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

like image 25
Bùi Việt Thành Avatar answered Oct 14 '22 23:10

Bùi Việt Thành