module.run() and $state.go() angularJS

后端 未结 7 2269
猫巷女王i
猫巷女王i 2021-02-12 12:38

I might have some kind of missunderstanding. Im using angular ui router and i have the next issue:

I have the next State provider:

angular
.module(\'Main         


        
7条回答
  •  情书的邮戳
    2021-02-12 13:03

    So i've made it work the different way, ive used $stateChangeStart and now my run function looks like this:

    angular
    .module('MainApp')
    .run(['$rootScope', '$state', '$cookieStore', 'principal',
        function ($rootScope, $state, $cookieStore, principal) {
            var cookies = $cookieStore.get('user');
            if (cookies) {
                principal.Authenticate(cookies.split(' ')[0], cookies.split(' ')[1]);
            }
            $rootScope.$on('$stateChangeStart',
                function (event, toState) {
                    if ((toState.name === "register") && (principal.isAuthenticated)) {
                        event.preventDefault();
                        $state.go('main', { userId: principal.identity().userId });
                    }
                });
        }
    ]);
    

    But i still dont know why $state.go() didnt work...

提交回复
热议问题