【问题标题】:callback in $http({method: 'GET', url: $scope.url})$http({method: 'GET', url: $scope.url}) 中的回调
【发布时间】:2013-10-21 17:54:07
【问题描述】:
$scope.fetchQA = function() {

    $scope.url = 'js/jsons/QA.json';

    $http({method: 'GET', url: $scope.url}).
      success(function(data, status, headers, config) {
        $scope.QA = data;
    });
  }

  $scope.fetchQA();

  function x(){
    alert(QA);
  }

如何使用function x 作为$http.get 的回调?或者有没有其他方法可以确保 x() 只有在接收到fetchQA 中的数据后才会执行?

【问题讨论】:

    标签: http angularjs get


    【解决方案1】:

    把它放在你的逻辑之后的回调中:

    $http({method: 'GET', url: $scope.url}).
      success(function(data, status, headers, config) {
        $scope.QA = data;
        x();
    });
    

    【讨论】:

    • 当你这样说的时候!我想知道为什么我没有想到它! :)
    • @SangramSingh -- 一直在发生,尤其是当你开始考虑异步时。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2017-08-26
    • 1970-01-01
    • 2011-09-25
    • 2020-10-19
    • 2012-08-18
    • 2010-09-20
    相关资源
    最近更新 更多