AngularJS:何时使用服务,而不是工厂 [英] AngularJS : When to use service instead of factory

查看:186
本文介绍了AngularJS:何时使用服务,而不是工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

请容忍我在这里。我知道有其他的答案,如:
  <一href=\"http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory\">Angular.js:服务提供商VS VS工厂?


但是我还是想不通,当你使用的服务超过工厂。

这是我可以告诉工厂通常用于创建可被多个控制器被称为通用的功能:<一href=\"http://stackoverflow.com/questions/11324202/angularjs-common-controller-functions?lq=1\">AngularJS - 通用控制器的功能

该角文档似乎preFER厂家对服务。他们甚至指服务,当他们使用的工厂,更是混乱! <一href=\"http://docs.angularjs.org/guide/dev_guide.services.creating_services\">http://docs.angularjs.org/guide/dev_guide.services.creating_services

因此,当将一用的服务?

有什么,这只是可能的,或者更容易与服务做了什么?

有什么不同之处在于那张幕后?性能/内存的差异?

下面是一个例子。除了声明的方法,他们似乎相同的,我不明白为什么我会做一VS其他。 http://jsfiddle.net/uEpkE/

更新:从托马斯的回答似乎在暗示,服务是与私有方法更复杂的逻辑简单的逻辑和工厂,所以我更新了小提琴code以下,似乎既能够支持私有函数?

  myApp.factory('fooFactory',函数(){
    VAR fooVar;
    VAR addHi =功能(富){fooVar ='嗨'+ foo的; }    返回{
        setFoobar:功能(富){
            addHi(富);
        },
        getFoobar:功能(){
            返回fooVar;
        }
    };
});
myApp.service('FooService接口',函数(){
    VAR fooVar;
    VAR addHi =功能(富){fooVar ='嗨'+富;}    this.setFoobar =功能(富){
        addHi(富);
    }
    this.getFoobar =功能(){
        返回fooVar;
    }
});功能MyCtrl($范围,FooService接口,fooFactory){
    fooFactory.setFoobar(fooFactory);
    fooService.setFoobar(FooService接口);
    // foobars =fooFactory嗨FooService接口
    $ scope.foobars = [
        fooFactory.getFoobar(),
        fooService.getFoobar()
    ];
}


解决方案

说明

您得到了不同的东西在这里:

第一:


  • 如果您使用的是服务,您将获得函数的实例这个
    关键词)。

  • 如果您使用的是工厂,你会得到的由返回的值
    调用函数引用
    (返回工厂语句)。

REF:的<一个href=\"http://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory\">angular.service VS angular.factory

二:

请记住在AngularJS所有供应商(价值不变,服务工厂)是单身!

第三:

使用一个或其他(服务或工厂)约code风格。
但是,在AngularJS在常用的方法是使用工厂

为什么呢?


  

因为 工厂方法获取对象为AngularJS依赖注入系统中最常见的方式,它是非常灵活的,可以包含复杂的创作逻辑。因为工厂都定期的功能,我们还可以利用一个新的词汇范围的模拟私有变量,这是非常有用的,因为我们可以隐藏特定的服务实施细则。


REF 的<一个href=\"http://rads.stackoverflow.com/amzn/click/1782161821\">http://www.amazon.com/Mastering-Web-Application-Development-AngularJS/dp/1782161821).


用法

服务:的可能是共享是通过简单地追加()来注入的函数引用调用有用的实用功能非常有用。也可以用 injectedArg.call(本)或类似的运行。

工厂:的可能是返回,然后可以new`ed创建实例的类功能非常有用

因此​​,使用一个工厂当你有复杂的逻辑在服务和你不想暴露这种复杂性即可。

在其他情况下,如果您想返回一个服务的一个实例只是使用服务

但你会随着时间的推移,你会在80%的人使用,我认为案件工厂看看。

有关详细信息:<一href=\"http://blog.manishchhabra.com/2013/09/angularjs-service-vs-factory-with-example/\">http://blog.manishchhabra.com/2013/09/angularjs-service-vs-factory-with-example/


更新:

优秀张贴在这里:
<一href=\"http://iffycan.blogspot.com.ar/2013/05/angular-service-or-factory.html\">http://iffycan.blogspot.com.ar/2013/05/angular-service-or-factory.html


  

如果你想你的函数的被称为像一个正常的功能,使用
   工厂。如果你想用新的实例化的功能
  运营商使用的服务。如果你不知道其中的差别,使用的工厂。



更新:

AngularJS队做他的工作,并给予了解释:
http://docs.angularjs.org/guide/providers

而从这个页面:


  

工厂和服务是最常用的配方。它们之间唯一的区别是,服务的配方适用于自定义类型的对象更好,而工厂可以生产的JavaScript原语和功能。


Please bear with me here. I know there are other answers such as: Angular.js: service vs provider vs factory?

However I still can't figure out when you'd use service over factory.

From what I can tell factory is commonly used to create "common" functions that can be called by multiple Controllers: AngularJS - Common controller functions

The Angular docs seem to prefer factory over service. They even refer to "service" when they use factory which is even more confusing! http://docs.angularjs.org/guide/dev_guide.services.creating_services

So when would one use service?

Is there something that is only possible or much easier done with service?

Is there anything different that goes on behind the scenes? Performance/memory differences?

Here's an example. Other than the method of declaration, they seem identical and I can't figure out why I'd do one vs the other. http://jsfiddle.net/uEpkE/

Update: From Thomas' answer it seems to imply that service is for simpler logic and factory for more complex logic with private methods, so I updated the fiddle code below and it seems that both are able to support private functions?

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()
    ];
}

解决方案

Explanation

You got different things here:

First:

  • If you use a service you will get the instance of a function ("this" keyword).
  • If you use a factory you will get the value that is returned by invoking the function reference (the return statement in factory).

ref: angular.service vs angular.factory

Second:

Keep in mind all providers in AngularJS (value, constant, services, factories) are singletons!

Third:

Using one or the other (service or factory) is about code style. But, the common way in AngularJS is to use factory.

Why ?

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."

(ref: http://www.amazon.com/Mastering-Web-Application-Development-AngularJS/dp/1782161821).


Usage

Service : Could be useful for sharing utility functions that are useful to invoke by simply appending () to the injected function reference. Could also be run with injectedArg.call(this) or similar.

Factory : Could be useful for returning a ‘class’ function that can then be new`ed to create instances.

So, use a factory when you have complex logic in your service and you don't want expose this complexity.

In other cases if you want to return an instance of a service just use service.

But you'll see with time that you'll use factory in 80% of cases I think.

For more details: http://blog.manishchhabra.com/2013/09/angularjs-service-vs-factory-with-example/


UPDATE :

Excellent post here : 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."


UPDATE :

AngularJS team does his work and give an explanation: http://docs.angularjs.org/guide/providers

And from this page :

"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:何时使用服务,而不是工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆