Are Angularjs services singleton?

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

Tags:

Angularjs