angular 的ui.router 定义不同的state 对应相同的url
Angular UI Router: Different states with same URL?
The landing page of my app has two states: home-public
, home-logged-in
. Now I want to show both states on the same URL, but let the controller and template depend on the user session (is the user logged in or not?).
Is there a way to achieve this?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
You could have a base state that controls which state to load, and you could simply have the child stated of that base state not have urls:
.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) {
...........
}
})
Now in the controller of your base state (home
) you can check if the user is logged in or not, and use $state.go()
to load an appropriate state.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步