Javascript 如何等待angular http方法完成

Javascript 如何等待angular http方法完成,javascript,angularjs,Javascript,Angularjs,这是我从服务器获取数据的函数 function getAll_Tables() { $scope.tableList = []; $http({ method : 'GET', url : '/abc/GetTables', headers : {'Content-Type' : 'application/json'} }).success(function(data) { $('#T_LoaderSpinner').hide(); i

这是我从服务器获取数据的函数

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.")
    });
} 

我的表格列表未更新-如何在此处更新表格列表?

这里是一种更舒适的方式,请使用promoises

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

这里有一个更舒适的方法,使用promotises

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

您可能需要返回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()
是异步的,因此在使用它的函数中,当您执行
警报时,数据尚未提取。你需要等待这个承诺完成。我建议作出以下修改:

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.")
    });
}

您的问题是
getAll_Tables()
是异步的,因此在使用它的函数中,当您执行
警报时,数据尚未提取。你需要等待这个承诺完成。我建议作出以下修改:

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.")
    });
}

你能再解释一下吗?表是否未从后端填充数据?或者您希望对数据执行一些性能测试,但它不会改变?如果您可以创建一个
jsfiddle
链接来产生问题,这会更好。您的表格列表可能会更新,但不会在您发出警报时更新。您的getAll_ScoringTables执行异步$http调用,您的警报不会等待该调用完成Yes devqon。。。。但如何在警报中等待更新结果…?您能解释更多吗?表是否未从后端填充数据?或者您希望对数据执行一些性能测试,但它不会改变?如果您可以创建一个
jsfiddle
链接来产生问题,这会更好。您的表格列表可能会更新,但不会在您发出警报时更新。您的getAll_ScoringTables执行异步$http调用,您的警报不会等待该调用完成Yes devqon。。。。但是如何在警报中等待更新的结果…?+1到这个-if
$scope.getRuleList()
也是异步的,你肯定应该使用这个答案。我同意你的答案,但在我的场景中,我在多个地方使用getAll_Tables()。。。然后我必须在所有地方更新代码。+1到这个-if
$scope.getRuleList()
也是异步的,你肯定应该使用这个答案。我同意你的答案,但在我的场景中,我在多个地方使用getAll_Tables()。。。然后我必须在所有地方更新代码。@sumitchoudhary您可以将数据作为回调的参数发送回,然后在回调中处理它(或在第二个示例中的success函数中)@sumitchoudhary您可以将数据作为回调的参数发送回,然后在回调中处理它(或在第二个示例中的成功函数中)