【问题标题】:AngularJS wait for all async calls inside foreach finish?AngularJS 等待 foreach 中的所有异步调用完成?
【发布时间】:2015-11-06 18:12:59
【问题描述】:

我尝试在完成 foreach 循环后获取所有数据,但不知何故总是得到 []

 $scope.temp = [];
    angular.forEach(seasonStageIds, function (seasonStageId, index) {

        playerService.getPlayerProfile(seasonStageId, $stateParams.params.playerSename).then(function (data) {

            playerService.getPlayerPositionPerSeason(seasonStageId, data.data.Id).then(function (data) {
                console.log(data);
                _.chain(data)
                 .groupBy('Position')
                 .map(function (value, key) {

                     var goals = _.reduce(value, function (memo, num) {
                         return memo + num.Goals;
                     }, 0);
                     var apps = _.reduce(value, function (memo, num) {

                         return memo + num.Apps;
                     }, 0);
                     var assists = _.reduce(value, function (memo, num) {
                         return memo + num.Assist;
                     }, 0);
                     var ratings = _.reduce(value, function (memo, num) {
                         return memo + num.Rating;
                     }, 0);

                     var position = {
                         Position: key,
                         Apps: apps,
                         Assists: assists,
                         Ratings: ratings,
                         Goals: goals                        
                     };       
                     console.log(position);          >> correct data
                     $scope.temp.push(position);
                 });
            });


        });
    });


    $q.all($scope.temp).then(function (data) {
        console.log( data);   >> show []
    })

我做错了什么? 谢谢大家。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    $q.all 仅适用于承诺! IE。等待所有的 Promise 完成,然后运行 ​​'then' 函数。您正在传递一个数组作为参数。要获得您想要的,请尝试以下操作:

    $scope.temp = [];
    angular.forEach(seasonStageIds, function (seasonStageId, index) {
    
        var promise =  playerService.getPlayerProfile(seasonStageId, $stateParams.params.playerSename).then(function (data) {
    
            return playerService.getPlayerPositionPerSeason(seasonStageId, data.data.Id).then(function (data) {
                return _.chain(data)
                 .groupBy('Position')
                 .map(function (value, key) {
    
                     var goals = _.reduce(value, function (memo, num) {
                         return memo + num.Goals;
                     }, 0);
                     var apps = _.reduce(value, function (memo, num) {
    
                         return memo + num.Apps;
                     }, 0);
                     var assists = _.reduce(value, function (memo, num) {
                         return memo + num.Assist;
                     }, 0);
                     var ratings = _.reduce(value, function (memo, num) {
                         return memo + num.Rating;
                     }, 0);
    
                     var position = {
                         Position: key,
                         Apps: apps,
                         Assists: assists,
                         Ratings: ratings,
                         Goals: goals                        
                     };       
    
                     return position;
                 });
            });
        });
    
        $scope.temp.push(promise);
    });
    
    
    $q.all($scope.temp).then(function (data) {
        console.log( data);
    });
    

    例如http://jsfiddle.net/HB7LU/19677/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-16
      • 2021-11-13
      • 2011-10-04
      • 1970-01-01
      相关资源
      最近更新 更多