如何在角度js中阻止http请求,以便我可以在角度js中的下一行使用$ http响应。
这里$ http对象不会在下一行返回结果,以便我可以在fullcalender上传递一个javascript库的$ http响应。
这里$ scope.data返回空值。
下面的示例代码
$http.get('URL').success(function(data){ $scope.data = data; }); $.fullCalender({ data: $scope.data });
您可以使用
promises。
原文链接:https://www.f2er.com/angularjs/144270.html这里是一个例子:
$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,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) }); }