2013-10-21 68 views
1
$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的回調?或者有沒有其他方法可以確保只有在接收到fetchQA中的數據後才能執行x()?

回答

2

把它放在你的回調邏輯之後:

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

當u把它像!我想知道爲什麼我沒有想到它! :) –

+1

@SangramSingh - 隨時都會發生,特別是當你開始思考異步時。 :d – tymeJV