Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

angularjs - angular.service vs angular.factory(angular.service vs angular.factory)

I have seen both angular.factory() and angular.service() used to declare services;

(我见过用于声明服务的angular.factory()angular.service() ;)

however, I cannot find angular.service anywhere in official documentation.

(但是,我在官方文档中找不到 angular.service 。)

What is the difference between the two methods?

(这两种方法有什么区别?)

Which should be used for what (assuming they do different things)?

(应该用什么(假设他们做不同的事情)?)

  ask by jacob translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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 :

(Factory :将调用您编写的函数 (构造函数):)

  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: (比如编写一个服务函数来公开一个公共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: (或使用工厂函数公开公共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.

(这是因为myInjectedService必须始终是一个对象,myInjectedFactory可以是一个对象,一个函数引用或任何值。)

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

((但是你应该首先考虑使用这种类型的模式,因为你的控制器中的对象会创建难以模拟测试的难以跟踪的依赖关系。最好让服务管理一组对象你比使用new()狡猾的。))


One more thing, they are all Singletons... (还有一件事,他们都是单身人士......)

Also keep in mind that in both cases, angular is helping you manage a singleton.

(还要记住,在这两种情况下,angular都可以帮助您管理单身人士。)

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

((除了工厂只返回一个数字或字符串之类的值。在这种情况下,您将始终获得相同的值,但不是引用。))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...