我有一个主控制器为我的应用程序 - 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路由器安全状态
发布评论