AngularJS:如何清除URL中的查询参数?

ale*_*wan 133 angularjs

我的AngularJS应用程序需要访问用户的LinkedIn个人资料.为了做到这一点,我需要将用户重定向到包含回调redirect_uri参数的LinkedIn URL,该参数将告诉LinkedIn将用户重定向回我的webapp并在URL中包含"代码"查询参数.这是传统的Oauth 2.0流程.

一切都很好,除了LinkedIn将用户重定向回以下URL:

http://localhost:8080/?code=XXX&state=YYY#/users/123/providers/LinkedIn/social-sites
Run Code Online (Sandbox Code Playgroud)

我想?code=XXX&state=YYY从URL中删除,以使其干净.用户不需要查看我从LinkedIn重定向收到的查询参数.

我尝试过$location.absUrl($location.path() + $location.hash()).replace(),但它会在URL中保留查询参数.

我也无法使用提取查询参数,例如"代码" ($location.search()).code.好像有吗?在上面的URL#之前欺骗Angular.

Jul*_*ius 149

我用

$location.search('key', null)
Run Code Online (Sandbox Code Playgroud)

因为这不仅会删除我的密钥,还会将其从URL上的可见性中删除.

  • 这导致chrome中的后退按钮循环,因为后退按钮返回到//example.com?key=value,但角度转发到//example.com.建议? (2认同)
  • 这听起来有点像代码问题.Angular不应该转发任何东西,除非你有一些方法之王.此外,添加.replace()会替换当前的历史记录条目. (2认同)
  • 不要使用窗口,而是使用Angular的$ window.单元测试会更容易.更多:https://docs.angularjs.org/api/ng/service/$window (2认同)

ale*_*wan 105

我最终得到了AngularJS论坛的答案.有关详情,请参阅此主题


该链接指向Google网上论坛帖子,该帖子难以阅读且无法提供明确答案.要删除URL参数,请使用

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


Rah*_*sai 68

要删除所有查询参数,请执行:

$location.search({});
Run Code Online (Sandbox Code Playgroud)

要删除一个特定的查询参数,请执行:

$location.search('myQueryParam', null);
Run Code Online (Sandbox Code Playgroud)

  • 适用于Angular 1.5. (2认同)

bas*_*rat 29

要清除项目,请将其删除并调用$$撰写

    if ($location.$$search.yourKey) {
        delete $location.$$search.yourKey;
        $location.$$compose();
    }
Run Code Online (Sandbox Code Playgroud)

派生自angularjs源:https://github.com/angular/angular.js/blob/c77b2bcca36cf199478b8fb651972a1f650f646b/src/ng/location.js#L419-L443

  • 辉煌!工作就像一个魅力. (2认同)
  • 谢谢,这很有效……有趣的是,IE8 喜欢设置/组合该 URL,然后随后加载它。我敢肯定,这就是为什么首选正确的路由以及为什么我们都是坏人的原因。:P (2认同)

小智 27

您可以使用以下命令删除特定查询参数:

delete $location.$$search.nameOfParameter;
Run Code Online (Sandbox Code Playgroud)

或者,您可以通过将搜索设置为空对象来清除所有查询参数:

$location.$$search = {};
Run Code Online (Sandbox Code Playgroud)

  • 我不知道为什么,但是这个解决方案在切换页面时效果不好(参数可以重新出现,即使执行了`$ location.$$ search = {}`也是如此).@basarat解决方案运作良好. (2认同)

Fre*_*ric 26

在撰写本文时,正如之前提到的@Bosh,html5mode必须是true为了能够设置$location.search()并将其反映回窗口的可视URL.

有关详细信息,请参阅https://github.com/angular/angular.js/issues/1521.

但是,如果 html5mode 是,true您可以轻松地清除URL的查询字符串:

$location.search('');
Run Code Online (Sandbox Code Playgroud)

要么

$location.search({});
Run Code Online (Sandbox Code Playgroud)

这也将改变窗口的可视URL.

(经测试,在AngularJS版本1.3.0-rc.1html5Mode(true).)

  • `this.is.realy =直截了当&& its.working.for.me` :) (5认同)

Bos*_*osh 12

html5mode= 时需要使它工作false

所有其他的答案只有工作的时候角的html5modetrue.如果你在外面工作html5mode,那么$location只提到你的哈希中的"假"位置 - 因此$location.search无法查看/编辑/修复实际页面的搜索参数.

这是一个解决方法,角度加载之前插入页面的HTML中:

<script>
  if (window.location.search.match("code=")){
    var newHash = "/after-auth" + window.location.search;
    if (window.history.replaceState){
      window.history.replaceState( {}, "", window.location.toString().replace(window.location.search, ""));
    }
    window.location.hash = newHash;
  }
</script>
Run Code Online (Sandbox Code Playgroud)

  • 我避免将html5Mode配置为true的另一种方法是创建查询字符串,使其在**?**之前包含**#**.所以有`myapp /#?client = clientName`而不是`myapp /?client = clientName` (2认同)

fel*_*ppe 6

如果您想移动到另一个 URL 并清除查询参数,只需使用:

$location.path('/my/path').search({});
Run Code Online (Sandbox Code Playgroud)