Skip to content Skip to sidebar Skip to footer

Failed Async Call In Resolve

I've added the following to my app.config in order to resolve a globally needed variable before allowing any of my routes to kick off their associated controller. This has been wor

Solution 1:

For global resolvers patching when isn't the best solution (method patching is an extreme case and should be avoided if possible), generally putting them to $routeChangeStart listener is cleaner and gives more control.

The errors should be handled in $routeChangeError, listeners get rejection reason as the fourth argument:

var ignored = ['/resolver-error'];

  $rootScope.$on('$routeChangeStart', function (e, to) {
    if (ignored.indexOf(to.originalPath) >= 0)
      return;

    to.resolve = to.resolve || {};
    // can be overridden by route definition
    to.resolve.availableCodes = to.resolve.availableCodes || 'availableCodesService';
  });

  $rootScope.$on('$routeChangeError', function (e, to, from, reason) {
    if (reason === ...) {
      $location.path('/resolver-error');
    }
  });

This allows to skip this resolver on at least one route which is supposed to provide a feedback on resolution error.

Post a Comment for "Failed Async Call In Resolve"