简体   繁体   English

如何知道用户是否输入了无效的$ routeparams

[英]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". 我需要防止用户输入“ id”以外的参数。

eg: 例如:

example.com/?id="something" is accepted example.com/?id="something"被接受

example.com/?junk="something" should be rejected. example.com/?junk="something"应该被拒绝。

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: 详细说明这些人在说什么,您可以在初始化应用(在run函数或根控制器中)或routeUpdate侦听器中执行以下操作:

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). 显然,您需要将$ location注入到DI中(将其作为参数添加到该函数范围中-您的侦听器所在的范围)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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