Javascript .run在$http回调之前启动。角度用户界面路由器

Javascript .run在$http回调之前启动。角度用户界面路由器,javascript,angularjs,routing,angular-ui-router,transition,Javascript,Angularjs,Routing,Angular Ui Router,Transition,我正在使用ui路由器1.0.0.X及其新标准。 我的代码: 用于登录的服务,将数据保存在存储器中,确定它是否存在并清除 app.factory('AuthService', ['$http', '$cookies', '$rootScope', function ($http, $cookies, $rootScope) { var service = {}; // Authenticates throug a rest service service.authen

我正在使用ui路由器1.0.0.X及其新标准。
我的代码:

  • 用于登录的服务,将数据保存在存储器中,确定它是否存在并清除

    app.factory('AuthService', ['$http', '$cookies', '$rootScope',
    function ($http, $cookies, $rootScope) {
    
        var service = {};
    
        // Authenticates throug a rest service
        service.authenticate = function (email, password, callback) {
    
            $http.post('endPoints/login.php', {
                    email: email,
                    password: password
                })
                .then(function (response) {
    
                    callback(response);        
                });
        };
    
        // Creates a cookie and set the Authorization header
        service.setCredentials = function (response) {
    
            $rootScope.globals = response;
    
            $http.defaults.headers.common['Authorization'] = 'Bearer ' + response;
            $cookies.put('globals', $rootScope.globals);
        };
    
        // Checks if it's authenticated
        service.isAuthenticated = function () {
    
            console.log("If TRUE callback not worked yet!!",$cookies.get('globals') === undefined);
    
            return !($cookies.get('globals') === undefined);
        };
    
        // Clear credentials when logout
        service.clearCredentials = function () {
            $rootScope.globals = undefined;
            $cookies.remove('globals');
                          console.log("CLEAN coockies globals",$cookies.get('globals'));
    
            $http.defaults.headers.common.Authorization = 'Bearer ';
        };
    
        return service;
    }
    ]);
    
  • 配置并运行。在那里,我们可以使用以下方法:

    angular.module('myApp', 
    ['ui.router',
    'ngCookies'
    ])
    .config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $urlRouterProvider.otherwise('/resumen');
            $stateProvider
                    .state("dashboard", {
                        url: "/dashboard",
                        templateUrl: "partials/dashboard.html",
                        controller: "dashCtrl",
                        data: {
                            authRequired: true
                        }
                    })
                    .state("login", {
                        url: "/login",
                        templateUrl: "partials/login.html",
                        controller: "loginController"
                    })
        }])
    
    .run(['$rootScope', '$transitions', '$state', '$cookies', '$http', 'AuthService',
        function ($rootScope, $transitions, $state, $cookies, $http, AuthService) {
    
            // keep user logged in after page refresh
            $rootScope.globals = $cookies.get('globals') || {};
            $http.defaults.headers.common['Authorization'] = 'Bearer ' + $rootScope.globals;
    
            $transitions.onStart({
                to: function (state) {
                    return state.data != null && state.data.authRequired === true;
                }
            }, function () {
            console.log("I'm transition.onStart and i'm alive!!!");
                if (!AuthService.isAuthenticated()) {
                    return $state.target("autorize");
                }
            });
        }]);
    
在仪表板状态下,我使用数据来标记只有在经过身份验证时才可访问的状态。 然后,在.run上,我使用转换来检查授权状态

  • 我的控制器$绑定到ng click指令的scope.logIn:

        $scope.logIn = function () {
        AuthService.authenticate($scope.loginInfo.email, $scope.loginInfo.password, function (callback) { 
    
        console.log("CALLBACK!",callback); //-> callback from server. Always true
    
        AuthService.setCredentials(callback);   
    });
    
    }

所有这些都按预期工作,但在第一次单击ng时,我收到了以下信息:

因此,.run方法和转换在ng从服务器单击回调之前运行

第二次ng click数据已在前一次ng click服务器请求中设置,因此一切正常!

所以,问题是如何避免那些可怕的两次点击通话

我参与代码的$transition文档:

相关帖子:

使用$state.go('target_state')解决;指令,而不是使用$state.go('target_state')解析ui srefs;指令而不是ui sref