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