UI- Router — run function on every route change — where does the state name live?

痴心易碎 提交于 2019-12-30 06:27:11

问题


Using Angularjs and UI-Router, trying to run a function every time state changes

$rootScope.$on('$stateChangeStart',
        function(toState){
            if(toState !== 'login')
            UsersService.redirect();
        })

I put this in .run() and i can successfully log out toState every time route changes. However, i can't seem to find the property which has the name of the state that we are going to. If someone can tell me where to find that, i think i should be in good shape.


回答1:


Ended up with this and it does what i want.

$rootScope.$on('$stateChangeStart',
        function(event, toState, toParams, fromState, fromParams){
            if(toState.name !== 'login' && !UsersService.getCurrentUser()) {
            event.preventDefault();
            $state.go('login');
            }
        });


来源:https://stackoverflow.com/questions/24357286/ui-router-run-function-on-every-route-change-where-does-the-state-name-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!