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

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

问题描述

我被卡住的东西,我认为应该是直截了当。我需要从三个不同的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.

在最简单的形式我的code是这样的:

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.

我想要做的是立即添加code:

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调用之后,试图立即执行它们,即使承诺没有得到解决。

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 是给我一个承诺对象有三个功能:错误成功&安培; 然后。我不知道做什么,在这种情况下,与该做的。我需要所有三个单成功的功能。我需要所有三个来解决,然后处理在每个数据,然后将结果分配到的角度的模型

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的功能,全部

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

这方法是有点清洁。

下面是链接docementation: http://docs.angularjs.org/api/ng.$q

Here is link to docementation: http://docs.angularjs.org/api/ng.$q

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

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