2017-08-16 82 views
0

我正在使用angular.js与C#web服务,我需要增加ng - 重复逐项显示给用户,因为数据更新,做到这一点我试图在循环中使用$ http.get来刷新每个项目中的数据。但它不工作

for (var i = 0; i < conditions.length; i++) { 
     var configFullAsset = { 
      params: { 
       Field: Field, 
       SAM_ConnectionString: SAM_ConnectionString, 
       TreeElemId: conditions[i][0], 
       ConditionDate: conditions[i][1] 
      } 
     }; 
     $http.get('application.asmx/getExtFullAssetHealth', configFullAsset) 
      .success(function (responseExt) { 
       console.log("Element: ", i); 
       if (i == 0) 
       { 
        $scope.showData = responseExt; 

        $scope.fullAssetTable_loading = false; 
        $scope.fullAssetTable_loaded = true; 
       } 
       else 
        $scope.showData = $scope.showData.concat(responseExt); 

       //console.log("Data item: ", $scope.showData[i].Tag); 

       $scope.fullData = $scope.showData; 
       $scope.filterData(customFilter); 
      }) 
      .catch(function (err) { 
       console.log("Error get object: ", err); 
      }) 
      .finally(function() { 
       // Hide loading spinner whether our call succeeded or failed. 
       //$scope.loading = false; 


       $scope.fullData = $scope.showData; 
       $scope.filterData(customFilter); 
       $scope.fullAssetTable_loading = false; 
       $scope.fullAssetTable_loaded = true; 

       console.log($scope.fullData); 
      }); 
    } 
+1

你能指定什么不工作,任何错误消息,预期的结果是什么?用这些信息帮助你更容易。 –

回答

0

的主要问题到你的代码是休耕:你用我作为指数的成功方法,但不是你所期望的,因为循环结束,直到你的第一次成功将被调用。

你可以建立在第一阶段这样的要求,是更容易阅读:

function buildRequests() { 
    return conditions.map(function(condition) { 
     var configFullAsset = { 
      params: { 
       Field: Field, 
       SAM_ConnectionString: SAM_ConnectionString, 
       TreeElemId: condition[0], 
       ConditionDate: condition[1] 
      } 
     }; 

     return $http.get('application.asmx/getExtFullAssetHealth', configFullAsset); 
    }); 
} 

比你可以处理所有这样的请求:

function handleRequests() { 
    var requests = buildRequests(); 
    $q.all(requests) 
     .then(handleRequests) 
     .catch(function(error) { 
      console.log(error); 
     }) 
     .finally(function() { 
      $scope.fullData = $scope.showData; 
      $scope.filterData(customFilter); 
      $scope.fullAssetTable_loading = false; 
      $scope.fullAssetTable_loaded = true; 
     }); 
} 

比遍历每个结果做出更改:

function handleResults(results) { 
    results.forEach(function(response, i) { 
     console.log("Element: ", i); 
     if (i == 0) 
     { 
      $scope.showData = response; 

      $scope.fullAssetTable_loading = false; 
      $scope.fullAssetTable_loaded = true; 
     } 
     else 
      $scope.showData = $scope.showData.concat(response); 

     //console.log("Data item: ", $scope.showData[i].Tag); 

     $scope.fullData = $scope.showData; 
     $scope.filterData(customFilter); 
    }); 
} 

不要忘记将$ q注入为依赖注入ction。

+0

它为我工作,tks! – danilovroque