ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

how capture route error when $stateChangeError ?

Open jcsena opened this issue 11 years ago • 3 comments

config route

$stateProvider.state('categories All', {
      url: '/categories',
      templateUrl: 'categories/views/index.html',
      resolve: {
       loadRoute: app.loadRoute
     }
    });

init error

app.loadRoute = function ($q, $timeout,$location,Global) {
  // Initialize a new promise
  var deferred = $q.defer();
    // Is country select
   $timeout(deferred.reject);
  return deferred.promise;
};

catch error

function(event, toState, toParams, fromState, fromParams) {

  });

now how catch route error?

Example

/categories?test=1&value=2 how catch this route? and not the config route

thx.

jcsena avatar Feb 27 '15 21:02 jcsena

Errors in a resolve will cause $state to fire the $stateChangeError event.

So you could listen to the event:

    module.run(['$rootScope', function($rootScope){
      $rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
        if(toState.name == 'Categories')
        {
           // Do Something
        }
      });
    }]);

Also I wouldn't use a space in your state name. I don't even know if this is supported.

acollard avatar Mar 03 '15 16:03 acollard

Thanks for the answer, @acollard. @jcsena Please take future support questions to StackOverflow, thanks.

nateabele avatar Mar 31 '15 21:03 nateabele

Great thank you so much 👍 @acollard

AlameerAshraf avatar Jan 06 '18 15:01 AlameerAshraf