我的应用的目标网页有两种状态:home-public
,home-logged-in
。现在我想在同一个URL上显示两个状态,但让控制器和模板依赖于用户会话(用户是否登录?)。
有没有办法实现这个目标?
答案 0 :(得分:51)
你可以有一个基本状态来控制要加载的状态,你可以简单地让那个基本状态的孩子没有网址:
.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()
加载适当的状态。
修改强>
按照承诺,a working plunk。
答案 1 :(得分:0)
我不确定两个州是否可以拥有相同的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';
}
})
我没有尝试,但它应该工作。