Angular routing is not broadcasting the $routeChangeSuccess

81 Views Asked by At

I'm working with AngularJS and angular_1_router : https://docs.angularjs.org/api/ngComponentRouter.

I want when navigating between routes to check if the next routing is allowed or not, if it's not allowed the user will be redirected to 403 page.

In the AngularJs API :

$routeChangeSuccess

Broadcasted after a route change has happened successfully. The resolve dependencies are now available in the current.locals property.

So I did as following :

$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
    $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(function (path) {
        if (path !== null) {
            _handleForbiddenRouting(path, path.urlPath).then(function(result){
                if(result !== null) {
                    $rootRouter.navigate([result]);
                }
            });
        }
    }, function (error) {
        loggerFactory.error('An error occured during navigation.', error);
    });
});

The _handleForbiddenRouting function will check if the route I'm navigating into is allowed if it's not the user will be redirected to the route name in result (which is 403 page).

In some cases there are some views that are not resolved (the view is not rendered), because there was an error during the call of $routerOnActivate (XHR error for example), in this case the angular_1_router is not broadcasting the event $routeChangeSuccess, thus the function _handleForbiddenRouting is not executed.

How can I solve this ?

Edit:

Please dont mix between $routeProvider and ngComponentRouter.

0

There are 0 best solutions below