angular——访问多个http调用的数据——如何解决promise [英] angular -- accessing data of multiple http calls - how to resolve the promises

查看:25
本文介绍了angular——访问多个http调用的数据——如何解决promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些我认为应该直截了当的事情.我需要从三个不同的 ajax 调用中获取数据,组合并处理所有三个,并将结果数组显示给用户.

I am getting stuck on something I think should be straight forward. I need to take data from three different ajax calls, combine and process all three, and display the resulting array to the user.

以最简单的形式,我的代码如下所示:

In its simplest form my code looks like this:

function giftControler ($scope, $http) {
  var names = $http.get("names.json"),
      naughty = $http.get("naughty.json"),
      nice = $http.get("nice.json");

我知道我的变量被分配给了承诺,而不是实际结果,并且 http 请求已被传递到事件队列.如果我遵循这些可执行语句,这些变量将是未定义的.我不明白如何等待这些承诺得到解决才能继续处理它们.

I understand that my variables are assigned to promises, not actual results, and that the http request have been passed to the event queue. If I follow these with executable statements these variables will be undefined. I don't understand how to wait for these promises to resolve in order to continue processing them.

我想做的是立即添加代码:

What I would like to do is immediately add the code:

      for (var i=0; i<names.length; i++){
        for (var j=0; j<nice.length; j++){
          if (names[i] === nice[j]){
            names[i] = names[i] + "--Yay!!";
          };
        };
      };                
      $scope.kids = names;

问题是我不能像解析数组一样处理承诺.Javascript 会在 http 调用后立即看到这些 for 语句并尝试立即执行它们,即使 promise 尚未解决.

The problem is that I can't just work off of the promises as if they were resolved arrays. Javascript will see these for statements right after the http calls and try to execute them immediately, even though the promises have not been resolved.

我陷入困境的是 $http api 给了我一个具有三个函数的 promise 对象:errorsuccess &然后.在这种情况下,我不确定该怎么办.我需要所有三个成功函数.我需要所有三个来解析,然后处理每个中的数据,然后将结果分配给一个角度模型.

Where I get stuck is that the $http api is giving me a promise object with three functions: error, success & then. I am not sure what to do with that in this case. I need a single success function for all three. I need all three to resolve, then process the data in each, and then assign the result to an angular model.

我确定我遗漏了一些明显的东西,但有人有什么建议吗?在我的实际工作中,我有多个 ajax 调用多个数据源和大量处理(比较值、排序、连接等)来得出一个好的数据集合,但我无法通过这个问题.

I am sure I am missing something obvious, but does anyone have a suggestion? In my real work I have several ajax calls multiple data sources and a lot of processing (comparing values, sorting, concatenating, etc) to come up with one good data collection, but I can't get passed this issue.

谢谢,

推荐答案

你可以使用$q的函数'all':

You can use $q's function 'all':

function giftControler ($scope, $http, $q) {
  var names = $http.get("names.json"),
      naughty = $http.get("naughty.json"),
      nice = $http.get("nice.json");
  $q.all([names, naughty,nice]).then(function(arrayOfResults) { 
      ... This callback would be called when all promised would be resolved
    });

这种方式比较干净.

这里是文档的链接:http://docs.angularjs.org/api/ng.$q

这篇关于angular——访问多个http调用的数据——如何解决promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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