AngularJS:UI路由器安全状态 [英] AngularJS: ui-router secured states

查看:143
本文介绍了AngularJS:UI路由器安全状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主控制器为我的应用程序 - AppCtrl 并使用UI的路由器。我怎样才能让安全的国家?

I have one main controller for my app - AppCtrl and use ui-router. How can I make secured states?

$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
    var authorization = toState.data.authorization;

    if(!Security.isAuthenticated() && authorization != false)
        $location.path('/login');
});

例如我想使图书作者在国家保证和登录状态并不安全。

For example I want to make books and authors states secured, and login state not secured.

.state('login', {
                url: '/login',
                templateUrl: /**/,
                controller: /**/,
                data: {
                    authorization: false
                }
            })
.state('books', {
                url: '/books',
                templateUrl: /**/,
                controller: /**/,
                data: {
                    authorization: true
                }
            })
.state('authors', {
                url: '/authors',
                templateUrl: /**/,
                controller: /**/,
                data: {
                    authorization: true
                }
            })

Security.isAuthenticated()函数返回boolean值。当我打开 /书籍一切完美,页面将被重定向到 /登录,当重定向后,我打开 /作者,页面加载和它的内容被显示,但浏览器的网址是 /登录,所以页面重定向,但不知何故,它是内容所示。

Security.isAuthenticated() function returns boolean. When I open /books everything works perfectly, page are being redirected to /login, when after redirecting I open /authors, page loads and it's content are shown, but browser's url is /login, so page being redirected, but somehow it's content are shown.

推荐答案

想通了,我不得不prevent打开旁边的路线,并转到登录状态。做了一些修改和所有完美的作品。

Figured out that I have to prevent opening next route, and go to login state. Made some changes and all works perfectly.

if (!Security.isAuthenticated() && authorization != false){
        event.preventDefault();
        $state.go('login');
    }

这篇关于AngularJS:UI路由器安全状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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