4

Is it possible to only allow certain choices in the routeProvider with angularjs?

For example, if I have:

$routeProvider.when('/:action/view/', {

Can I only allow ('basketball', 'baseball') as actions for this to be set as a match?

KingKongFrog
  • 13,946
  • 21
  • 75
  • 124

2 Answers2

0

you can use redirectTo attribute of your route to redirect users in case of not allowed route call :

$routeProvider.when('/:action/view/',{
    redirectTo: function(routeParams, locationPath, locationSearch){
        var allowed = ['baseball','basketball'];        //allowed action param

        if(allowed.indexOf(routeParams.action) != -1){  //route is allowed don't redirect
            return null;
        }
        else{                                           //route is denied redirect to home
            return '/home';
        }
    }
}
Freezystem
  • 4,494
  • 1
  • 32
  • 35
0

Not tested, but you could listen on $routeChangeStart, which gets called before the route changes, use the "next" and "current" route arguments to figure out if the future route is allowed. If not, do a $location.path to set them back to the current route itself.

rajasaur
  • 5,340
  • 2
  • 27
  • 22