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

How to know if user has entered invalid $routeparams

本文关键字:无效 routeparams 输入 何知道 用户 是否      更新时间:2023-09-26

我可以使用

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

我需要防止用户输入除"id"以外的参数。

,

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

应拒绝example.com/?junk="something"

有办法吗?

详细说明这些人所说的评论,你可以在初始化应用程序(在run函数或根控制器中)或在routeUpdate监听器中做以下事情:

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

显然,您需要将$location注入到DI中(将其作为参数添加到该函数作用域中—您的侦听器在问题中的作用域中)。