javascript 从 AngularJS 工厂返回函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21223493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:25:36  来源:igfitidea点击:

Returning function from AngularJS factory

javascriptangularjs

提问by catrapture

I'm trying to understand what the purpose is of the return part of this AngularJS factory method means?

我试图了解这个 AngularJS 工厂方法的返回部分的目的是什么?

return {
    getMessages: getMessages
  };

What happens if we added a new method to this factory called getAnotherMessage(), would we need to update this return segment?

如果我们向这个工厂添加一个名为 getAnotherMessage() 的新方法会发生什么,我们是否需要更新这个返回段?

myModule.factory('HelloWorld', function($q, $timeout) {

  var getMessages = function() {
    var deferred = $q.defer();

    $timeout(function() {
      deferred.resolve(['Hello', 'world!']);
    }, 2000);

    return deferred.promise;
  };

  return {
    getMessages: getMessages
  };

});

回答by KayakDave

factoryis a providerconstructor:

factory是一个提供者构造函数:

factory(fn) - registers a service factory function, fn, that will be wrapped in a service provider object, whose $get property will contain the given factory function.

factory(fn) - 注册一个服务工厂函数 fn,它将被包装在一个服务提供者对象中,其 $get 属性将包含给定的工厂函数。

Thus when the factory is first loaded by Angular it executes the function that's passed in and stores whatever is returned as the provider.

因此,当 Angular 首次加载工厂时,它会执行传入的函数并存储作为提供者返回的任何内容。

In other words, the following is run once, and only once- during bootstrapping:

换句话说,以下内容运行一次,并且仅在引导期间运行一次:

var getMessages = function() {
   var deferred = $q.defer();

   $timeout(function() {
      deferred.resolve(['Hello', 'world!']);
   }, 2000);

   return deferred.promise;
 };

return {
   getMessages: getMessages
 };

The above gets a reference to the getMessagefunction and attaches it to the property getMessagesinside the returned singleton object.

上面得到了对该getMessage函数的引用,并将其附加到getMessages返回的单例对象内的属性上。

When the provider is then injected into your code, that returned object is what is passed in giving you access to the HelloWorld.getMessages function (and any other properties in the returned object).

当提供者随后被注入到您的代码中时,返回的对象就是传递给您访问 HelloWorld.getMessages 函数(以及返回对象中的任何其他属性)的内容。

So, yes, if you want to associate another property, such as a function, with the provider (that the factory constructs) you need to include it as a property of the returned singleton object:

所以,是的,如果您想将另一个属性(例如函数)与提供者(工厂构造的)相关联,您需要将其作为返回的单例对象的属性包含在内:

return {
   getAnotherMessage: function() { ... },
   getMessages: getMessages
 };

回答by hamster ham

You can also declare an empty object first and add functions into the object
and finally return the object.

也可以先声明一个空对象,然后在对象中添加函数
,最后返回对象。

 myModule.factory('HelloWorld', function($q, $timeout) {   
   var myobject = {};   
   myobject.getMessages = function() {    ...   };   
   myobject.getAnotherMessages = function() {    ...   };

   return myobject;   
 });