angular.service 与 angular.factory [英] angular.service vs angular.factory

查看:26
本文介绍了angular.service 与 angular.factory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过 angular.factory()angular.service() 用于声明服务;但是,我找不到 angular.service官方文档.

I have seen both angular.factory() and angular.service() used to declare services; however, I cannot find angular.service anywhere in official documentation.

这两种方法有什么区别?
哪个应该用于什么(假设他们做不同的事情)?

What is the difference between the two methods?
Which should be used for what (assuming they do different things)?

推荐答案

  angular.service('myService', myServiceFunction);
  angular.factory('myFactory', myFactoryFunction);


在我以这种方式告诉自己之前,我一直无法理解这个概念:


I had trouble wrapping my head around this concept until I put it to myself this way:

服务:您编写的功能将是新的-ed:

Service: the function that you write will be new-ed:

  myInjectedService  <----  new myServiceFunction()

工厂:您编写的函数(构造函数)将被调用:

Factory: the function (constructor) that you write will be invoked:

  myInjectedFactory  <---  myFactoryFunction()

你用它做什么取决于你,但有一些有用的模式......

What you do with that is up to you, but there are some useful patterns...

function myServiceFunction() {
  this.awesomeApi = function(optional) {
    // calculate some stuff
    return awesomeListOfValues;
  }
}
---------------------------------------------------------------------------------
// Injected in your controller
$scope.awesome = myInjectedService.awesomeApi();

或者使用工厂函数来公开一个公共 API:

Or using a factory function to expose a public API:

function myFactoryFunction() {
  var aPrivateVariable = "yay";

  function hello() {
    return "hello mars " + aPrivateVariable;
  }
  
  // expose a public API
  return {
    hello: hello
  };
}
---------------------------------------------------------------------------------
// Injected in your controller
$scope.hello = myInjectedFactory.hello();

或者使用工厂函数返回一个构造函数:

Or using a factory function to return a constructor:

function myFactoryFunction() {
    return function() {
        var a = 2;
        this.a2 = function() {
            return a*2;
        };
    };
}
---------------------------------------------------------------------------------
// Injected in your controller
var myShinyNewObject = new myInjectedFactory();
$scope.four = myShinyNewObject.a2();


使用哪个?...

你可以用两者完成同样的事情.但是,在某些情况下,工厂 为您提供了更大的灵活性,可以使用更简单的语法创建可注入对象.这是因为 myInjectedService 必须始终是一个对象,而 myInjectedFactory 可以是一个对象、一个函数引用或任何值.例如,如果您编写了一个服务来创建构造函数(如上面的最后一个示例),则必须像这样实例化它:


Which one to use?...

You can accomplish the same thing with both. However, in some cases the factory gives you a little bit more flexibility to create an injectable with a simpler syntax. That's because while myInjectedService must always be an object, myInjectedFactory can be an object, a function reference, or any value at all. For example, if you wrote a service to create a constructor (as in the last example above), it would have to be instantiated like so:

var myShinyNewObject = new myInjectedService.myFunction()

可以说比这更不受欢迎:

which is arguably less desirable than this:

var myShinyNewObject = new myInjectedFactory();

(但是首先您应该警惕使用这种类型的模式,因为new 控制器中的对象会创建难以跟踪的依赖项,这些依赖项难以模拟以进行测试.更好让服务为您管理一组对象,而不是使用 new() 巧妙地.)

(But you should be wary about using this type of pattern in the first place because new-ing objects in your controllers creates hard-to-track dependencies that are difficult to mock for testing. Better to have a service manage a collection of objects for you than use new() wily-nilly.)

还要记住,在这两种情况下,angular 都在帮助您管理单身人士.无论您注入服务或函数的位置或次数如何,您都将获得对同一对象或函数的相同引用.(除非工厂只返回一个数字或字符串之类的值.在这种情况下,您将始终获得相同的值,但不会获得引用.)

Also keep in mind that in both cases, angular is helping you manage a singleton. Regardless of where or how many times you inject your service or function, you will get the same reference to the same object or function. (With the exception of when a factory simply returns a value like a number or string. In that case, you will always get the same value, but not a reference.)

这篇关于angular.service 与 angular.factory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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