0

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.

Jonathan Anctil
  • 1,025
  • 3
  • 20
  • 44
Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
  • If a failure is happening during routing is preventing success, then try listening to [`$routeChangeStart`](https://docs.angularjs.org/api/ngRoute/service/$route#event-$routeChangeStart) broadcast instead. – Omar Einea Mar 19 '18 at 18:18
  • Use resolve functions to reject route changes. If all the promises are resolved successfully, the values of the resolved promises are injected and [`$routeChangeSuccess`](https://docs.angularjs.org/api/ngRoute/service/$route#$routeChangeSuccess) event is fired. If any of the promises are rejected the [`$routeChangeError`](https://docs.angularjs.org/api/ngRoute/service/$route#$routeChangeError) event is fired. For more information, see [AngularJS $routeProvider API Reference](https://docs.angularjs.org/api/ngRoute/provider/$routeProvider). – georgeawg Mar 19 '18 at 18:31
  • @OmarEinea there is no such event in the `angular_1_router`. – Renaud is Not Bill Gates Mar 19 '18 at 18:51
  • @georgeawg I'm working with the `ngComponentRouter` which is `angular_1_router` and not the `$routeProvider`, here is the API for `ngComponentRouter` : https://docs.angularjs.org/api/ngComponentRouter – Renaud is Not Bill Gates Mar 19 '18 at 19:14

0 Answers0