3

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?

3
  • What's wrong with using $urlRouterProvider.otherwise()? Commented Nov 4, 2015 at 3:51
  • I want a startup path and a fallback/otherwise path which is different from the startup path. Hence, the question. Commented Nov 4, 2015 at 3:54
  • Writing something like this in your module.config would make a trick. $urlRouterProvider.when('', '/dashboard'); Commented Nov 4, 2015 at 4:55

1 Answer 1

1

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');
        });
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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'; } }); ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.