您可以使用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