[英]Ui-Router otherwise rule with ng-admin
I have got a problem in an angular app, where I have integrated ng-admin to provide a simple admin panel. 我在Angular应用程序中遇到问题,我在其中集成了ng-admin以提供一个简单的管理面板。 I have also integrated Authentication and want to point to a login state, when there is no current session.
我还集成了身份验证,并希望在没有当前会话时指向登录状态。
Ng-admin itself sets ui-router's otherwise method to point directly to the dashboard. Ng-admin本身将ui-router的else方法设置为直接指向仪表板。 Even when setting my own
$urlRouterProvider.otherwise(...)
I am not able to force ui-router to use my otherwise function. 即使在设置自己的
$urlRouterProvider.otherwise(...)
我也无法强制ui-router使用我的$urlRouterProvider.otherwise(...)
函数。
I tried it like this: 我这样尝试过:
$urlRouterProvider.otherwise(function ($injector) {
console.log('Hello World?!');
state.go('login');
});
But the console.log is never printed... 但是console.log永远不会被打印...
I would like to intercept the state transition to the dashboard, before any dashboard-code is run, and redirect the user to the login page first. 在运行任何仪表板代码之前,我想拦截到仪表板的状态转换,然后首先将用户重定向到登录页面。
Any hints and ideas are greatly appreciated. 任何提示和想法都将不胜感激。
Thanks @user8175473 for pointing me into the right direction. 感谢@ user8175473为我指出正确的方向。
I have solved the problem using $on('$stateChangeStart)
as $on('$stateChangeSuccess)
is called after some state code is executed. 我已经解决了使用
$on('$stateChangeStart)
因为$on('$stateChangeSuccess)
执行某些状态代码后调用了$on('$stateChangeSuccess)
。
$rootScope.$on('$stateChangeStart', function(event, toState) {
if(toState.name !== 'login' && !service.isLoggedIn()) {
event.preventDefault();
$state.go('login');
}
});
Note that event.preventDefault()
is needed to cancel the state transition completely. 请注意,需要使用
event.preventDefault()
才能完全取消状态转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.