2014-05-10 19 views
0
$http({ 
url: "php/load.php", 
method: "GET", 
params: {'userId':userId} 
}).success(function(data, status, headers, config) { 
    $scope.data = data; 

}).error(function(data, status, headers, config) { 
}); 

$scope.data[0]= "something"; 

如何在ajax之上等待以完成加載然後加載$ scope.data [0] =「something」;?我試圖把它內$ http和低於$ scope.data =數據,但它似乎有衝突仍然..ajax在異步模式下的回調衝突

+2

爲什麼不在成功回調裏面做? –

+0

-1不是一個真正的問題。 [**如何問**](http://stackoverflow.com/questions/how-to-ask) – mate64

+0

簡單的答案:你不能。 –

回答

1
var promise1 = $http({ 
url: "php/load.php", 
method: "GET", 
async: false, 
params: {'userId':userId} 
}); 
$.when(promise1).then(
function(data, status, headers, config) { 
    $scope.data = data; 
}, 
function(data, status, headers, config) { 
} 
).done(function() { 
$scope.data[0]= "something"; 
}); 

沒有測試,但應該是正確的。如果不是,請查看jQuery.when()jQuery.promise()