AngularJS - 静默更改$ location - 删除查询字符串

par*_*ent 13 angularjs

有没有办法使用angular默默地更改url栏中的路径?

用户单击指向以下内容的电子邮件的链接:

/verificationExecuted?verificationCode=xxxxxx
Run Code Online (Sandbox Code Playgroud)

当页面加载时我想读取verificationCode然后清除它:

 if($location.path() == '/verificationExecuted'){
     this.registrationCode = this.$location.search()["verificationCode"];
     this.$location.search("verificationCode", null); //Uncomment but make this silent!

     if(registrationCode != null) {
         ....
     }
     else $location.path("/404");      
 }
Run Code Online (Sandbox Code Playgroud)

当我清除它时会发生什么,路由的剩余部分("/ verifyExecuted")仍然是路由重新触发,所以它再次出现没有verifyCode并直接进入404.

我想删除代码而不做任何其他事情.

Jos*_*osh 28

您始终reloadOnSearch可以在路线上设置选项false.

如果只查询字符串发生更改,它将阻止路由重新加载:

$routeProvider.when("/path/to/my/route",{
   controller: 'MyController',
   templateUrl: '/path/to/template.html',
   //Secret Sauce
   reloadOnSearch: false
});
Run Code Online (Sandbox Code Playgroud)


Shi*_*Sah 5

试试这个

$location.url($location.path())
Run Code Online (Sandbox Code Playgroud)

有关$ location的更多详细信息,请参阅文档