简体   繁体   中英

How to know if user has entered invalid $routeparams

I can access query url params using

$scope.$on('$routeUpdate', function () {    
    var id = $routeParams.id
   //check if user has entered any params other than "id".
   //if yes do someting
});

I need to prevent user from entering params other than "id".

eg:

example.com/?id="something" is accepted

example.com/?junk="something" should be rejected.

Is there any way for this?

Elaborating what the guys are saying the comments you could do something like the following where you are initialising your app (in the run function or in a root controller) or within your routeUpdate listener:

if($location.search.id){
    $location.search({id:$location.search.id}));
} else {
    $location.search({});
}

Obviously you'll need to inject the $location into the DI (add it as a param in that function scope - the scope your listener is in in your question).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM