我正在尝试使用规则函数设置.otherwise()来确定默认URL。由于尚未加载应用程序,因此我无法注入状态或读取路径,我真正需要做的是在.otherwise()中进行解析以检查我的UserService并为会话ping服务器的操作,因为我的UserService.isLoggedIn是未定义的。在真正需要诸如resolve之类的内容时,如何处理具有不同的默认URL?
看一下官方文档中的示例:http : //angular-ui.github.io/ui-router/site/#/api/ui.router.router。$ urlRouterProvider#methods_otherwise
// Example of using function rule as param
$urlRouterProvider.otherwise(function ($injector, $location) {
return '/a/valid/url';
});
您拥有$injector
,$location
因此可以随心所欲。如果需要,您甚至可以将其包装在易于阅读的可注入函数中:
function myOtherwiseFunction($state, UserService, Whatever) {
// check some $state stuff
if ($state.current) { /* blah */ }
// check some Whatever stuff
if (Whatever.myFancyLogic()) { /* blah */ }
if (UserService.isLoggedIn())
return "/app";
else
return "/login";
}
$urlRouterProvider.otherwise(function ($injector, $location) {
return $injector.invoke(myOtherwiseFunction);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句