Is it possible to define a startup path for ui-router that is different from $urlRouterProvider.otherwise(). Or is there maybe a trick to fool ui-router into navigating to a different path at startup?
1 Answer
You can always use the callback method of otherwise() to choose the default path based on the current one, eg
$urlRouterProvider.otherwise(function($injector, $location) {
return $location.path() === '/' ? '/startup' : '/fallback';
});
Otherwise, I'd just make a "home" or "root" state for the / URL and redirect elsewhere
$stateProvider.state('root', {
url: '/',
onEnter: function($state) {
// this is so you don't interrupt the current state transition
$state.transition.then(function() {
return $state.go('startup-state');
});
}
});
2 Comments
Rahul Gupta
The problem is '/' is a legitimate path. If the user navigates to '/' at a later time they get redirected again. Also, I just cannot check against the startup path to redirect to the fallback because if the user refreshes then the previous path is again the startup path and intended destination is also the startup path.
Rahul Gupta
While your answer was not exactly what I was looking for it gave me the idea need to solve the problem. The code I ended up with looks like this: ``` let startup = true; $urlRouterProvider.otherwise(function($injector, $location) { if (startup) { startup = false; return '/startup'; } else { return '/fallback'; } }); ```
$urlRouterProvider.otherwise()?