如何在angularjs解析中阻止服务调用

时间:2018-02-22 10:20:00

标签: javascript angularjs angular-promise

在我的angularJS应用程序中,我在该路由中有一条路由,我正在解析一些http服务调用。

 .when("/Dashboard", {
        controller: 'dashboardController',
        templateUrl: 'app/Dashboard/template/dashboard.html,
        resolve: {
           authorize: ['AuthService', function (authService) {
                return authService.validateSession();
            }],
           dashboardData: ['DashboardService', function (dashboardService) {
                 var deffered = $q.defer();
                dashboardService.getDashboardData(function (response) {
                    deffered.resolve(response);
                });
                return deffered.promise;
            }], 
        }
  }

这是我的AuthService:

    app.factory('AuthService', factory);
    factory.$inject = ['$http',  '$q', '$cookies', '$location', '$rootScope'];

    function authService($http, $routeParams, $q, $cookies, $location, $rootScope) {
     /***** removed the code for clarity *****/

     function validateUserSession(cb) {
            var deferred = $q.defer();
            if ($cookies.get("userId")) {
               checkUserSession.create(function (response) {
                    if (response.data == "Valid") {
                        deferred.resolve();
                    }
                    else {
                        var Url = "/Login";
                        var userId= $cookies.get('userId', { path: '/' });
                        if (userId) {
                            Url = Url + "/" + userId;
                        }
                        $location.path(Url);                        
                        deferred.resolve();
                    }
                })
            }
            else {
                var Url = "/Login";
                var userId= $cookies.get('userId', { path: '/' });
                if (userId) {
                    Url = Url + "/" + userId;
                }
                $location.path(Url);
                $q.reject();
            }
            return deferred.promise;
        };
   }

我想阻止第二次调用dashboardData解决,如果第一次调用授权将因$ cookies.get(" userId")返回undefined或checkUserSession服务返回无效而无效。如何做到这一点在angularjs?

0 个答案:

没有答案