簡體   English   中英

如何等待角度http方法完成

[英]how to wait for angular http method to complete

這是我從服務器獲取數據的功能

function getAll_Tables() {
  $scope.tableList = [];
  $http({
    method : 'GET',
    url : '/abc/GetTables',
    headers : {'Content-Type' : 'application/json'}
  }).success(function(data) {
      $('#T_LoaderSpinner').hide();
      if (data.StatusCode === 101) {
        if (data.Data != null) {
          for (var i = 0; i < data.Data.length; i++) {
            $scope.tableList.push(data.Data[i]);
          }
        }
      } else {
        alert("We could not process your request......Please try later.")
      }
    })
    .error(function(data) {
      $('#T_LoaderSpinner').hide();
      alert("We could not process your request......Please try later.")
    });
} 

我使用該數據的另一個功能是:

$scope.load_policy = function(id, type) {
  $scope.getRuleList();
  getAll_ScoringTables(); 
  alert(JSON.stringify($scope.tableList));
}

我的tableList沒有更新-如何在這里獲取更新的tableList?

這是一種更舒適的方式,使用促銷

var promise1 = $http({method: 'GET', url: 'a/pi-one-url', cache: 

'true'});
var promise2 = $http({method: 'GET', url: '/api-two-url', cache: 'true'});

$q.all([promise1, promise2]).then(function(data){
    console.log(data[0], data[1]);
});

您要么需要返回http請求,然后簡單地在加載策略中處理成功,要么可以使用回調來調用從成功方法中調用的匿名函數。 成功完成后,這將調用您的匿名函數

function getAll_Tables(callback) { 
    $scope.tableList = [];
    $http({
        method : 'GET',
        url : '/abc/GetTables',
        headers : {'Content-Type' : 'application/json'}
    }).success(function(data) { 
        $('#T_LoaderSpinner').hide();
        callback();
    }).error(function(data) { 
        $('#T_LoaderSpinner').hide();
        alert("We could not process your request......Please try later.")
    });
}  

$scope.load_policy = function(id, type) { 
    $scope.getRuleList(function() {
        getAll_ScoringTables(); 
        alert(JSON.stringify($scope.tableList));
    });
}

因此,其他選擇之一是

function getAll_Tables(callback) { 
    $scope.tableList = [];
    return $http({
        method : 'GET',
        url : '/abc/GetTables',
        headers : {'Content-Type' : 'application/json'}
    });
}  

$scope.load_policy = function(id, type) { 
    $scope.getRuleList.success(function() {
        getAll_ScoringTables(); 
        alert(JSON.stringify($scope.tableList));
    }).error(function() {
        alert('failed');
    });
}

您的問題是getAll_Tables()是異步的,因此在使用它的函數中,執行alert ,尚未提取數據。 您需要等待此承諾完成。 我建議進行以下更改:

function getAll_Tables() {
  $scope.tableList = [];
  return $http({ // ADDED RETURN HERE
    method : 'GET',
    url : '/abc/GetTables',
    headers : {'Content-Type' : 'application/json'}
  }).success(function(data) {
      $('#T_LoaderSpinner').hide();
      if (data.StatusCode === 101) {
        if (data.Data != null) {
          for (var i = 0; i < data.Data.length; i++) {
            $scope.tableList.push(data.Data[i]);
          }
          return; // ADDED RETURN HERE
        }
      } else {
        alert("We could not process your request......Please try later.")
      }
    })
    .error(function(data) {
      $('#T_LoaderSpinner').hide();
      alert("We could not process your request......Please try later.")
    });
}

所以在這里我們返回的$http功能出getAll_Tables()這樣我們就可以.then在其他功能,或者換句話說,等待該請求是與里面的代碼完成,才能繼續.then功能。

$scope.load_policy = function(id, type) {
  $scope.getRuleList();
  getAll_ScoringTables()
  .then(function() {
    alert(JSON.stringify($scope.tableList)); // tableList has the correct data now
  })
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM