在Angular JS中依次使用嵌套的$ http.get调用

时间:2018-04-20 07:36:00

标签: angularjs http scope http-get

我有这种情况,其中两个$ http.get调用是嵌套的。我从第一个调用得到结果,然后迭代第一个结果并将其传递给另一个$ http.get调用,最后我正在努力使整个作为ab数组的对象。我发现,整体并没有按顺序发生。有人可以帮助我吗?

  $scope.populateData = function()
      {
         $scope.infoWithStatus = [];

          $http.get("commonAppGet.jsp?sqlStr=select name from test where title_id=1").then(function(resp){
                  $scope.names = resp.data.d;
                  for(var i=0;$scope.names.length;i++){
                      infoObject= {};
                      var c1=0;c2=0; c3=0;c4=0;c5=0;
                      $scope.spocName = $scope.names[i].name;
                      infoObject.name=$scope.spocName;
                                $http.get("commonAppGet.jsp?sqlStr=select a.status as status from test1 where name='"+$scope.spocName+"'").then(function(resp){

                                   $scope.statusValues = resp.data.d;
                                    for(var i=0;i<$scope.statusValues.length;i++)
                                      {
                                          if($scope.statusValues[i].status==0)
                                            c1++;
                                          if($scope.statusValues[i].status==1)
                                              c2++;
                                          //some code for c3,c4,c5                    

                                       }
                                       infoObject.count1=c1;
                                       infoObject.count2=c2;                                                             
                                       infoObject.count3=c3;
                                       infoObject.count4=c4;
                                       infoObject.count5=c5;

                                });
                             $scope.infoWithStatus.push(infoObject); 
                       }
         });
 }

1 个答案:

答案 0 :(得分:2)

也许这就是你

我在第一个承诺中看到你遗漏了i < $scope.names.length

$scope.populateData = function()
    {
        $scope.infoWithStatus = [];
        var c1=0;c2=0; c3=0;c4=0;c5=0;
        $http.get("commonAppGet.jsp?sqlStr=select name from test where title_id=1").then(function(resp){
            $scope.names = resp.data.d;
            var listPromise = [];
            for(var i=0;i < $scope.names.length;i++){
                infoObject= {};
                $scope.spocName = $scope.names[i].name;
                infoObject.name=$scope.spocName;
                listPromise.push($http.get("commonAppGet.jsp?sqlStr=select a.status as status from test1 where name='"+$scope.spocName+"'"));
                $scope.infoWithStatus.push(infoObject); 
            }
            return Promise.all(listPromise);
        }).then(function(resp){
            for (var i = 0; i < resp.length; i++) {
                $scope.statusValues = resp[i].data.d;
                for(var i=0;i<$scope.statusValues.length;i++)
                {
                    if($scope.statusValues[i].status==0)
                    c1++;
                    if($scope.statusValues[i].status==1)
                        c2++;
                    //some code for c3,c4,c5                    

                }
                infoObject.count1=c1;
                infoObject.count2=c2;                                                             
                infoObject.count3=c3;
                infoObject.count4=c4;
                infoObject.count5=c5;
            }
        });
 }