简体   繁体   English

从ngRoute迁移到ui.router $ rootScope。$'$ stateChangeStart'上的$不触发

[英]migrate from ngRoute to ui.router $rootScope.$on '$stateChangeStart' not triggering

I am trying to create a authentication system. 我正在尝试创建一个身份验证系统。 I stubled upon this (plunk) tutorial which is great but uses ngRoute and i wish to stwitch to ui.router. 我在这个(插件教程上打了个基础, 很棒,但是使用ngRoute,我希望切换到ui.router。

After some hard time trying to do it by myself, i dedided to search a bit more to find this ui.router auth demo (plunk) . 经过一番艰苦的尝试,我决定进一步搜索以找到此ui.router auth demo(插件)

I replaced some stuff an now my code looks like this (plunk) . 我替换了一些东西,现在我的代码看起来像这样(插头)

I have successfully managed to replace ngRoute with ui.router but i have a small big problem: 我已经成功地用ui.router替换了ngRoute,但是我有一个小小的大问题:

The module.run method hits, but inside it I have the code below that never gets triggered (I just want the alert to pop so can move on to write the redirect logic). 遇到了module.run方法,但是在它里面,我下面的代码从未触发(我只想弹出警报,因此可以继续编写重定向逻辑)。

$rootScope.$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams) {
        alert("enter");

        // redirect to login page if not logged in and trying to access a restricted page
        // var restrictedPage = $.inArray($state.path(), ['/login', '/register']) === -1;
        // var restrictedPage = !($state.includes("login") || $state.includes("register"));

        var loggedIn = $rootScope.globals.currentUser;
        if (!loggedIn) {
            $state.go('login');
        }
    });

I have yet to figure out what I am doing wrong. 我还没有弄清楚我在做什么错。 Thank you for your time guys! 谢谢您的宝贵时间!

Try this version. 试试这个版本。

Basically, use $transitions instead of $rootScope.$on('$stateChangeStart' , because that has been deprecated. 基本上,使用$transitions而不是$rootScope.$on('$stateChangeStart' ,因为已经弃用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM