【问题标题】:Using nested $http.get calls in Angular JS sequentially在 Angular JS 中按顺序使用嵌套的 $http.get 调用
【发布时间】:2018-09-30 20:45:59
【问题描述】:

我有这种情况,其中两个 $http.get 调用是嵌套的。我从第一个调用中获取结果,然后迭代第一个结果并将其传递给另一个 $http.get 调用,最后我正在尝试完成作为对象数组的东西。我发现,这整个过程并没有按顺序发生。有人可以帮我吗?

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

【问题讨论】:

    标签: angularjs http scope http-get


    【解决方案1】:

    也许这就是你

    我看到你在第一个承诺中错过了i &lt; $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;
                }
            });
     }
    

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      相关资源
      最近更新 更多