AngularJS:UI路由器安全状态

编程入门 行业动态 更新时间:2024-10-23 21:30:27
本文介绍了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路由器安全状态

本文发布于:2023-11-23 01:38:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由器   状态   AngularJS   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!