[英]Angular.js ui-router call child states from onEnter() based on authentication with same url as parent
[英]Angular UI Router: Different states with same URL?
我的应用程序的登录页面有两种状态: home-public
, home-logged-in
。 现在我想在同一个URL上显示两个状态,但让控制器和模板依赖于用户会话(用户是否登录?)。
有没有办法实现这个目标?
你可以有一个基本状态来控制要加载的状态,你可以简单地让那个基本状态的孩子没有url:
.state('home', {
url: "/home",
templateUrl: "....",
controller: function($scope,$state,authSvc) {
if(authSvc.userIsLoggedIn()){
$state.go('home.loggedin')
}else{
$state.go('home.public')
}
}
})
.state('home.public', {
url: "",
templateUrl: "....",
controller: function($scope) {
...........
}
})
.state('home.loggedin', {
url: "",
templateUrl: "....",
controller: function($scope) {
...........
}
})
现在,在基本状态( home
)的控制器中,您可以检查用户是否已登录,并使用$state.go()
加载适当的状态。
编辑
按照承诺, 一个工作插件 。
我不确定两个州是否可以拥有相同的url
。 我能想到的可行选择是定义一个单一的州
$stateProvider.state('home', {
templateUrl: function (stateParams){
// Implement a logic that select what view from the server should be returned for logged in user and otherwise
//return 'templateurl';
}
})
我没试过,但它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.