angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.56k stars 3.01k forks source link

How redirect in resolve? #3708

Closed HASSANDL closed 6 years ago

HASSANDL commented 6 years ago

How redirect in resolve without use $timeout? (version 1.0.18)

christopherthielen commented 6 years ago

Resolve is intended to manage async data fetching. It wasn't intended to be used for redirect. For redirects, we generally use a transition hook. The transition hook has access to all the resolved data via the transition.injector().

If you want to redirect from a resolve, I suppose you could inject the current transition into the resolve, cancel the transition, and start a new one. It would look something like this:

resolve: ['$transition$', '$myService', function($transition$, $myService) {
  if ($myService.something) {
   $transition$.cancel();
   $transition$.router.stateService.go('otherplace');
  }
}]