AngularJS:等待异步调用 [英] AngularJS: Waiting for an asynchronous call

查看:172
本文介绍了AngularJS:等待异步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能换我的头周围AngularJS承诺的概念。

I can't wrap my head around AngularJS' concept of promises.

我有一个供应商:

var packingProvider = angular.module('packingProvider',[]);

packingProvider.provider('packingProvider',function(){
    return{
       $get: function($http){
           return{
              getPackings: function(){
                  $http.post('../sys/core/fetchPacking.php').then(function(promise){
                      var packings = promise.data;
                      return packings;
                  });
              }
           }
       }
   }
});

正如你所看到的,这提供了一个方法 getPackings(),它会返回一个对象

现在,如果我用,在我的主要应用程序来接收数据,调用将是异步的,造成了一个问题,我将不得不等待的数据:

Now, if I use that in my main application to receive the data, the call will be asynchronous, resulting in an issue where I would have to 'wait' for the data:

var packings = packingProvider.getPackings();

console.log(packings); // undefined

我将如何做到这一点,而不重构的过程中进入我的主控制器?

How would i do this without refactoring the process into my main controller?

推荐答案

这在您然后方法不返回返回值当你认为它是;它后来回来了。

The return value from within your then method isn't returning when you think it is; it's returning later.

在声明 VAR填料= packingProvider.getPackings(); 的返回值是不确定的,因为承诺从返回 $ HTTP 是异步的。这意味着,在调用 $ http.post 发生时,的未完成的,那么你的函数返回。在JS函数不返回任何返回undefined。在通话后完成并执行回填料; 这被退回无处

In the statement var packings = packingProvider.getPackings(); the return value is undefined because the promise returned from $http is asynchronous. That means that the call to $http.post happens, does not complete, then your function returns. In JS functions that don't return anything return undefined. The post call completes later and executes return packings; which gets returned to nowhere.

getPackings 方法或许应该归还从 $ http.post 的承诺直接。这样,任何code想要使用这种方法可以直接调用然后上的承诺和所设置的值,它需要的方式。在控制器,你可以直接将这一承诺的 $范围并在视图中使用它。这里是一个不错的职位,说明那种特定功能:<一href=\"http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/\">http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/

The getPackings method should probably return the promise from $http.post directly. That way any code that wants to use this method can call then on the promise directly and set the value the way it needs to. In a controller you could assign that promise directly to the $scope and use it in your view. Here's a nice post that explains that particular feature: http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/

顺便说一下,看起来你已经有了一个啰嗦的服务宣言那里。任何理由不把它缩短这样的事情?

Incidentally, it looks like you've got a long-winded service declaration there; any reason not to shorten it to something like this?

var module = angular.module('packing', []);

module.service('packing', function($http) {
  return {
    getPackings: function() {
      return $http.post('../sys/core/fetchPacking.php');
    }
  };
});

我是比较新的AngularJS,但我没有看到在所有类型的任何增益。 (=

I'm relatively new to AngularJS but I don't see any gain in all that typing. ( =

这篇关于AngularJS:等待异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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