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

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

问题描述

我看到这两个 angular.factory()并的 用于声明服务angular.service();不过,我找不到 angular.service 的任何地方在官方文档。

什么是两种方法之间的区别?应使用什么(假设他们做不同的事情)?


解决方案

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


我有麻烦缠绕我的头围绕这个概念,直到我把它放到自己是这样的:

服务:在功能的,你写会 -ed:

  myInjectedService< ----新myServiceFunction()

工厂:在功能的,你写(构造函数)将调用

  myInjectedFactory< --- myFactoryFunction()

您与该做的就是你的,但也有一些有用的模式...

如写的服务的函数来公开公共API:

 函数myServiceFunction(){
  this.awesomeApi =功能(可选){
    //计算一些东西
    返回awesomeListOfValues​​;
  }
}
-------------------------------------------------- -------------------------------
//在控制器注入
$ scope.awesome = myInjectedService.awesomeApi();

或使用的工厂的函数来公开公共API:

 函数myFactoryFunction(){
  VAR aPrivateVariable =耶;  你好功能(){
    返回你好火星+ aPrivateVariable;
  }  //公开公共的API
  返回{
    你好你好
  };
}
-------------------------------------------------- -------------------------------
//在控制器注入
$ scope.hello = myInjectedFactory.hello();

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

 函数myFactoryFunction(){
    返回功能(){
        变种一个= 2;
        this.a2 =功能(){
            返回* 2;
        };
    };
}
-------------------------------------------------- -------------------------------
//在控制器注入
VAR myShinyNewObject =新myInjectedFactory();
$ scope.four = myShinyNewObject.a2();


要使用哪一种?...

您可以完成与两个同样的事情。然而,在某些情况下的工厂的给你稍微更多的灵活性来创建的注射用简单的语法。这是因为同时myInjectedService必须始终是一个对象,myInjectedFactory可以是一个对象,一个功能参考,或在所有的任何值。例如,如果你写了一个服务,创建一个构造函数(如上面的最后一个例子),那就要被实例化,像这样:

  VAR myShinyNewObject =新myInjectedService.myFunction()

这可以说是不太理想的莫过于:

  VAR myShinyNewObject =新myInjectedFactory();

(但你应该警惕在首位使用这种模式,因为的新的的在控制器-ing对象创建难以跟踪依赖,很难模拟测试,更好的有一个服务管理对象的集合,你不是使用新()老谋深算愿意不愿意。)


还有一件事,他们都是单身...

也请记住,在这两种情况下,角度帮助你管理一个单身。无论在何处,或多少次你注入您的服务或功能,你会得到相同的参考同一对象或函数。 (有一家工厂直接返回像一个数字或字符串值例外。在这种情况下,你总是会得到相同的值,而不是引用。)

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:

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

Such as writing a service function to expose a public API:

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

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


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

(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.)


One more thing, they are all Singletons...

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 VS angular.factory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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