您可以使用promises。
這裏有一個例子:
$scope.myXhr = function(){
var deferred = $q.defer();
$http({
url: 'ajax.php',
method: 'POST',
data:postData,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
//if request is successful
.success(function(data,status,headers,config){
//resolve the promise
deferred.resolve('request successful');
})
//if request is not successful
.error(function(data,status,headers,config){
//reject the promise
deferred.reject('ERROR');
});
//return the promise
return deferred.promise;
}
$scope.callXhrAsynchronous = function(){
var myPromise = $scope.myXhr();
// wait until the promise return resolve or eject
//"then" has 2 functions (resolveFunction, rejectFunction)
myPromise.then(function(resolve){
alert(resolve);
}, function(reject){
alert(reject)
});
}
有什麼辦法可以避免這種情況? Javascript是單線程的,並且在等待響應時發出同步HTTP請求會阻止整個瀏覽器。這不應該是您的首選解決方案。 – GregL 2014-11-05 06:16:13
您不能簡單地將調用移動到fullCalendar到成功回調並設置數據等於回調的數據參數? – Scott 2014-11-05 06:17:00
當我在回調中使用fullcalendar數據時,它不顯示在模板上。 – Dipak 2014-11-05 06:24:59