0
我按如下方式尝试了$q
服务,但浏览器仍未等待响应。我已经花了将近一天的时间来找出解决方案,我也尝试了$timeout
。让浏览器在angularjs中等待api响应/ http呼叫响应
login.controller("loginCtrl", ['$scope', '$rootScope', '$filter', '$window', '$http', 'APIService', 'localStorageService', 'doAlert', '$translate', '$q', '$timeout',
function ($scope, $rootScope, $filter, $window, $http, APIService, localStorageService, doAlert, $translate, $q, $timeout) {
$scope.isLogOut = true;
$(window).unload(function() {
$rootScope.$broadcast('onUnload');
});
$scope.$on('onUnload', function (e) {
var deferred = $q.defer();
$http.get(url).
success(function (data, status, headers, config) {
deferred.resolve(data);
}).
error(function (data, status, headers, config) {
deferred.reject(status);
});
return deferred.promise;
});
只是在说一个例子:'$ http'already返回一个承诺。所以不应该需要'$ q'。 – Aer0
也试过这个。但没有运气$ http.get(url).success(function(response){ response.data; }); – user2617611
使用'$ http.then(函数(成功){...},函数(错误){...})'而不是成功和错误。您也可以查看文档:https://docs.angularjs.org/api/ng/service/$http – Aer0