Angular routeProvider set of declared parameters?

60 views Asked by At

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?

2

There are 2 answers

0
Freezystem On

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';
        }
    }
}
0
rajasaur On

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.