Joe Hawkins Joe Hawkins - 7 days ago 5
AngularJS Question

UI Router 1.0 state change events not working

I'm migrating my Angular 1.5 application to Angular UI Router 1.0-beta3 to use the component router. I'm listening for state change events on $rootScope but none of the events are fired.

Example:

angular
.module('app', [ 'ui.router' ])
.config(function($stateProvider) {
$stateProvider.state(
'dashboard',
{
url: '/dashboard',
component: 'dashboardComponent',
resolve: {
enabled: function() { throw new Error('NOT ENABLED'); }
}
});
})
.run(function($rootScope) {
$rootScope.$on('$stateChangeError', function() {
// Never called
});
});


Are state change events supported in component routing?

Answer

State change events are supported in component routing, but UI Router handles events differently in v1.0.

Try this:

...

.run(function($transitions) {
    $transitions.onError({}, function() {
        // Works like a charm
    });
});

See the docs for more info