- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Please bear with me here. I know there are other answers such as: AngularJS: Service vs provider vs factory
但是我仍然不知道你什么时候会使用工厂服务。
据我所知,工厂通常用于创建可由多个 Controller 调用的“通用”函数:Creating common controller functions
Angular 文档似乎更喜欢工厂而不是服务。他们在使用工厂时甚至提到“服务”,这更令人困惑! http://docs.angularjs.org/guide/dev_guide.services.creating_services
那么什么时候会使用服务呢?
有什么事情只有通过服务才能实现或者更容易完成吗?
幕后有什么不同的事情发生吗?性能/内存差异?
这是一个例子。除了声明方法之外,它们看起来相同,我不明白为什么我会做一个而不是另一个。 http://jsfiddle.net/uEpkE/
更新:从托马斯的回答来看,这似乎意味着服务是为了更简单的逻辑,而工厂是为了使用私有(private)方法来实现更复杂的逻辑,所以我更新了下面的 fiddle 代码,似乎两者都能够支持私有(private)函数?
myApp.factory('fooFactory', function() {
var fooVar;
var addHi = function(foo){ fooVar = 'Hi '+foo; }
return {
setFoobar: function(foo){
addHi(foo);
},
getFoobar:function(){
return fooVar;
}
};
});
myApp.service('fooService', function() {
var fooVar;
var addHi = function(foo){ fooVar = 'Hi '+foo;}
this.setFoobar = function(foo){
addHi(foo);
}
this.getFoobar = function(){
return fooVar;
}
});
function MyCtrl($scope, fooService, fooFactory) {
fooFactory.setFoobar("fooFactory");
fooService.setFoobar("fooService");
//foobars = "Hi fooFactory, Hi fooService"
$scope.foobars = [
fooFactory.getFoobar(),
fooService.getFoobar()
];
}
最佳答案
你在这里得到了不同的东西:
第一:
this
”关键词)。 引用: angular.service vs angular.factory
第二:
请记住,AngularJS 中的所有提供者(值、常量、服务、工厂)都是单例!
第三:
使用其中之一(服务或工厂)与代码风格有关。但是,AngularJS 中的常见方法是使用工厂。
为什么?
Because "The factory method is the most common way of getting objects into AngularJS dependency injection system. It is very flexible and can contain sophisticated creation logic. Since factories are regular functions, we can also take advantage of a new lexical scope to simulate "private" variables. This is very useful as we can hide implementation details of a given service."
(引用:http://www.amazon.com/Mastering-Web-Application-Development-AngularJS/dp/1782161821)。
<小时/>服务:对于共享实用函数非常有用,只需将 ()
附加到注入(inject)的函数引用即可调用这些实用函数。也可以使用 injectedArg.call(this)
或类似工具运行。
工厂:对于返回“类”函数非常有用,然后可以对其进行新建以创建实例。
因此,当您的服务中有复杂的逻辑并且您不想暴露这种复杂性时,请使用工厂。
在其他情况下如果您想返回服务实例,只需使用服务。
但随着时间的推移,你会发现我认为 80% 的情况下你都会使用工厂。
了解更多详情:http://blog.manishchhabra.com/2013/09/angularjs-service-vs-factory-with-example/
<小时/>更新:
这里的优秀帖子: http://iffycan.blogspot.com.ar/2013/05/angular-service-or-factory.html
<小时/>"If you want your function to be called like a normal function, use factory. If you want your function to be instantiated with the new operator, use service. If you don't know the difference, use factory."
更新:
AngularJS 团队做了他的工作并给出了解释: http://docs.angularjs.org/guide/providers
从这个页面:
"Factory and Service are the most commonly used recipes. The only difference between them is that Service recipe works better for objects of custom type, while Factory can produce JavaScript primitives and functions."
关于AngularJS : When to use service instead of factory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18939709/
我是一名优秀的程序员,十分优秀!