UI-路由器——每次路由变化时运行函数——状态名称在哪里? [英] UI- Router -- run function on every route change -- where does the state name live?

查看:15
本文介绍了UI-路由器——每次路由变化时运行函数——状态名称在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

解决方案

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');
            }
        });

这篇关于UI-路由器——每次路由变化时运行函数——状态名称在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆