Angularjs是单身的服务? [英] Angularjs are service singleton?
问题描述
在参考我读
最后,要认识到所有的角度服务是非常重要的
应用单身。这意味着有只有一个实例
每个喷油器给定服务。
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.
但这个简单的code似乎不是一个单身
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 ?
更新1
可能我不是在棚里最锐利的工具
但作为当@Khanh回复这将是一个更好的
解释在参考它不是很清楚了。
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.
更新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这是一个单身!
BOOM it's a singleton !
更新3
但是,如果你不喜欢
'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'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.
这篇关于Angularjs是单身的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!