Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  58] [ 3]  / answers: 1 / hits: 17778  / 10 Years ago, mon, april 28, 2014, 12:00:00

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?


More From » angularjs

 Answers
8

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.



EDIT



As promised, a working plunk.


[#71271] Friday, April 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alessandrol

Total Points: 286
Total Questions: 107
Total Answers: 109

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
;