加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS:何时使用服务而不是工厂

发布时间:2020-12-17 09:32:50 所属栏目:安全 来源:网络整理
导读:Please bear with me here. I know there are other answers such as: 07000 然而,我仍然不能弄清楚什么时候使用服务工厂。 从我可以告诉工厂通常用于创建“通用”功能,可以由多个控制器调用:AngularJS – Common controller functions Angular文档似乎更

Please bear with me here. I know there are other answers such as:
07000

然而,我仍然不能弄清楚什么时候使用服务工厂。

从我可以告诉工厂通常用于创建“通用”功能,可以由多个控制器调用:AngularJS – Common controller functions

Angular文档似乎更喜欢工厂服务。他们甚至参考“服务”,当他们使用工厂,这更令人困惑! http://docs.angularjs.org/guide/dev_guide.services.creating_services

所以什么时候使用服务?

有什么东西,只有可能或更容易做服务吗?

在幕后有什么不同吗?性能/内存差异?

这里有一个例子。除了声明的方法,他们似乎相同,我不知道为什么我会做一个vs另一个。 http://jsfiddle.net/uEpkE/

更新:从托马斯的答案似乎暗示服务是更简单的逻辑和工厂更复杂的逻辑与私有方法,所以我更新了下面的小提琴代码,似乎两者都能支持私有函数?

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”
关键词)。
>如果你使用工厂,你将得到返回的值
调用函数引用(工厂中的return语句)。

ref: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)。

用法

服务:通过简单地将append()附加到注入的函数引用,可以用于共享有用的调用的效用函数。也可以与injectArg.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.”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读