简体   繁体   English

然后Angular $ http解析_.each中的多个调用

[英]Angular $http then resolve for multiple calls in _.each

I am trying to fetch multiple data from $http inside _.each and show the combined data in $scope.tasksData. 我试图从_.each中的$ http中获取多个数据,并在$ scope.tasksData中显示组合数据。

But the problem is _.each is executed later and returned null first, Please guide how to resolve this. 但问题是_.each稍后执行并首先返回null,请指导如何解决此问题。

Thanks 谢谢

var tasksList, processingStageId;

var apiURL = "http://localhost:9080/caseDetails/" + $stateParams.caseId + "?_=1461046349867";



var stagesList = $http.get(apiURL).then(function(resdata, status, headers, config) {

return resdata;

}).then(function(stagesData) {

stageId = stagesData.data.childStages;
var allTasksData = [];
var self = this;

_.each(stageId, function(item) {
    var stagesApiURL = "http://localhost:9080/stageDetails/" + item.stage.stageId;

    $http.get(stagesApiURL).then(function(taskData, status, headers, config) {
        //_.extend(allTasksData, taskData.data);            
        allTasksData.push(taskData.data);

    });
});

return allTasksData;

}).then(function(allTasksData) {
console.log("Hello");

_.each(allTasksData, function(data) {
    $scope.tasksData.concat(data);
});
});

For get data after resolve all request, use $q.all([prom, prom, prom]) 要在解决所有请求后获取数据,请使用$ q.all([prom,prom,prom])

.then(function(stagesData){
    var stageId = stagesData.data.childStages;
    var tasks = [];

    _.each(stageId, function(item){
        var stagesApiURL = "http://localhost:9080/stageDetails/" + item.stage.stageId;
        var req = $http.get(stagesApiURL).then(function(taskData, status, headers, config){            
            return taskData.data;
        });
        tasks.push(req);
    });

    //Merge all promise request. Resolve when all request resolved
    return $q.all(tasks);

})
/**
 * allTasksData - [taskData.data, taskData.data , ...]
 */
.then(function(allTasksData){
  ...
});

You need to inject a $q service and use $q.all function 您需要注入$q服务并使用$q.all函数

var stagesList = $http.get(apiURL).then(function(resdata, status, headers, config) {
  return resdata;
})
.then(function(stagesData) {
  stageId = stagesData.data.childStages;

  var self = this;

  return $q.all(
    _.map(stageId, function(item) {
      var stagesApiURL = "http://localhost:9080/stageDetails/" + item.stage.stageId;

      return $http.get(stagesApiURL).then(function(taskData, status, headers, config) {
          return taskData.data;
      });
    })
  );
})
.then(function(allTasksData) {
  console.log("Hello");

  _.each(allTasksData, function(data) {
      $scope.tasksData.concat(data);
  });
});

Small explanation: we use map instead of each to generate an array of promises. 小解释:我们使用map而不是each来生成一个promises数组。 Then we pass an array of promises to the $q.all function which returns a new promise with an array of taskData.data as a parameter. 然后我们将一个promises数组传递给$q.all函数,该函数返回一个带有taskData.data数组作为参数的新promise。

$q.all() is there to help you. $q.all()可以帮到你。

According to documentation, it: 根据文件,它:

Combines multiple promises into a single promise that is resolved when all of the input promises are resolved. 将多个promise组合成一个promise,在解析所有输入promise时解析。

You register all your calls and then when all of them are resolved, you do what you need with your data. 您注册所有呼叫,然后在解决所有呼叫后,您可以根据数据执行所需操作。

Here is an example fiddle (not mine) with how it works. 这是一个示例小提琴 (不是我的),它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM