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

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

问题描述

请多多包涵.我知道还有其他答案,例如:AngularJS:服务 vs 提供者 vs 工厂

Please bear with me here. I know there are other answers such as: AngularJS: Service vs provider vs factory

但是我还是不知道你什么时候会使用服务而不是工厂.

据我所知,工厂通常用于创建可由多个控制器调用的通用"函数:创建通用控制器功能

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

Angular 文档似乎更喜欢工厂而不是服务.他们在使用工厂时甚至会提到服务",这更令人困惑!http://docs.angularjs.org/guide/dev_guide.services.creating_services

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

那么什么时候会使用服务?

有什么事情只有通过服务才能做到或者更容易做到吗?

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?

这是一个例子.除了声明方法之外,它们似乎相同,我无法弄清楚为什么我要这样做.http://jsfiddle.net/uEpkE/

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:

第一:

  • 如果您使用服务,您将获得函数的实例(this"关键词).
  • 如果您使用工厂,您将获得返回的值调用函数引用(工厂中的返回语句).
  • 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 与 angular.factory

第二:

请记住,AngularJS 中的所有提供者(值、常量、服务、工厂)都是单例的!

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

第三:

使用其中一种(服务或工厂)是关于代码风格的.但是,AngularJS 中常见的方式是使用工厂.

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

为什么?

因为 工厂方法是将对象引入AngularJS依赖注入系统的最常见方式.它非常灵活,可以包含复杂的创建逻辑.由于工厂是常规函数,我们还可以利用新的词法作用域来模拟私有"变量.这非常有用,因为我们可以隐藏给定服务的实现细节."

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

Service : 可用于共享实用程序函数,只需将 () 附加到注入的函数引用即可调用这些实用程序函数.也可以使用 injectedArg.call(this) 或类似工具运行.

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 : 可用于返回一个类"函数,然后可以对其进行 new`ed 以创建实例.

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

因此,当您的服务中有复杂的逻辑并且您不想暴露这种复杂性时,请使用工厂.

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

但随着时间的推移,你会发现我认为在 80% 的情况下你会使用工厂.

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

更多详情: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

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

从这个页面:

Factory 和 Service 是最常用的配方.它们之间的唯一区别是 Service recipe 更适合自定义类型的对象,而 Factory 可以生成 JavaScript 原语和函数."

"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天全站免登陆