Skip to content Skip to sidebar Skip to footer

Angularjs [ui-router] Urlrouteprovider.when() Versus Resolve For Routing To Child States

In my app I have a state called 'dashboard' with multiple child states .state('dashboard', { url: '/dashboard', abstract: true, // resolve async objects before controll

Solution 1:

Which child state the user will be redirected to depends on his/her session cookie, and I was curious where the appropriate place was to do the redirecting.

In general, if you want to drive navigation based on app state or session state, I would suggest you use a service to intercept the $stateChangeStart event and send them where they should be.

So, in your app's module.run callback, you could say:

$rootScope.$on('$stateChangeStart', function(event, toState) {
  if (toState.name === 'dashboard') {
    event.preventDefault();

    switch (myCondition) {
      case 1:
        $state.go('dashboard.splash');
        break;
      case 2:
        $state.go('dashboard.podSelector');
        break;
      default:
        $state.go('somewhereElse');
        break;
    }
  }
});

This way, you don't let the transition take costly time and energy by going to a state that you never intend to stay on, and you don't rely on $urlRouter.when.


Post a Comment for "Angularjs [ui-router] Urlrouteprovider.when() Versus Resolve For Routing To Child States"