2016-11-08 59 views
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; 
    }); 
+0

只是在说一个例子:'$ http'already返回一个承诺。所以不应该需要'$ q'。 – Aer0

+0

也试过这个。但没有运气$ http.get(url).success(function(response){ response.data; }); – user2617611

+0

使用'$ http.then(函数(成功){...},函数(错误){...})'而不是成功和错误。您也可以查看文档:https://docs.angularjs.org/api/ng/service/$http – Aer0

回答

0

这里是我使用回调从我的工具处理数据回

function get(url, callback, obj) { 
    $http.get(url, obj) 
    .then(function (response) { 
     callback(response.data); 
    }, function() { 
     notification.error(PLEASE_CONTACT_SYS_ADMIN); 
    }); 
} 
+0

我越来越angular.min.js:81 ReferenceError:回调未定义。你可以用我的Angular $ scope详细说明一下。$ on('onUnload')事件处理程序。这种新的。 – user2617611