AngularJS:如何清除URL中的查询参数?
我的AngularJS应用程序需要访问用户的LinkedIn个人资料。 为了做到这一点,我需要将用户redirect到LinkedIn URL,其中包含一个callbackredirect_uri参数,该参数会告诉LinkedIn将用户redirect到我的Web应用程序,并在URL中包含“代码”查询参数。 这是一个传统的Oauth 2.0stream程。
除了LinkedIn将用户redirect到以下URL之外,一切都很好:
http://localhost:8080/?code=XXX&state=YYY#/users/123/providers/LinkedIn/social-sites  我想从URL中删除?code=XXX&state=YYY ,以使其变得干净。 用户不需要看到我从LinkedInredirect收到的查询参数。 
 我尝试$location.absUrl($location.path() + $location.hash()).replace() ,但它保留在URL中的查询参数。 
 我也无法使用($location.search()).code提取查询参数,例如“code”。 好像有? 在上面的URL中#之前欺骗Angular。 
我用
 $location.search('key', null) 
这不仅会删除我的密钥,还会将其从URL的可见性中删除。
我最终从AngularJS论坛得到了答案。 详情请参阅此主题
该链接指向的Google网上论坛线程很难阅读,并且没有提供明确的答案。 使用删除URL参数
 $location.url($location.path()); 
要删除所有查询参数,请执行:
 $location.search({}); 
要删除一个特定的查询参数,请执行:
 $location.search('queryParam', null); 
要清除项目,请将其删除并拨打$$撰写
  if ($location.$$search.yourKey) { delete $location.$$search.yourKey; $location.$$compose(); } 
派生自angularjs源码: https : //github.com/angular/angular.js/blob/c77b2bcca36cf199478b8fb651972a1f650f646b/src/ng/location.js#L419-L443
您可以使用以下命令删除特定的查询参数:
 delete $location.$$search.nameOfParameter; 
或者,您可以通过将search设置为空对象来清除所有查询参数:
 $location.$$search = {}; 
 在撰写本文的时候,正如@Bosh先前提到的,为了能够设置$location.search()并将其reflection回窗口的可视URL, html5mode必须为true 。 
有关更多信息,请参阅https://github.com/angular/angular.js/issues/1521 。
 但是, 如果 html5mode 为true ,则可以使用以下方法轻松清除URL的查询string: 
 $location.search(''); 
要么
 $location.search({}); 
这也会改变窗口的可视url。
  (使用html5Mode(true)在AngularJS版本1.3.0-rc.1testing) 
 当html5mode = false时需要使它工作? 
 所有其他的答案只有当Angular的html5mode为true html5mode 。 如果你在html5mode之外工作,那么$location只是指你散列中的“假”位置,所以$location.search不能看到/编辑/修正实际页面的search参数。 
这是一个解决方法, 在angular度加载之前插入到页面的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> 
只是使用
 $location.url(); 
代替
 $location.path(); 
如果你正在使用路由参数,只需清除$ routeParams
 $routeParams= null; 
如果你想移动到另一个URL并清除查询参数,只需使用:
 $location.path('/my/path').search({}); 
如果您立即处理参数,然后移动到下一页,则可以在新位置的末尾放置一个问号。
例如,如果你会做$ location.path('/ nextPage');
你可以这样做:$ location.path('/ nextPage?');
 我已经尝试了以上的答案,但无法让他们的工作。 我唯一的代码是$window.location.search = '' 
 我可以用这一行replace所有查询参数: $location.search({}); 
 易于理解和简单的方法来清除它们。 
接受的答案为我工作,但我需要深入一点,以解决与后退button的问题。
 我注意到,如果我使用<a ui-sref="page({x: 1})">链接到一个页面,然后删除查询string$location.search('x', null) ,我不我的浏览器历史logging中没有额外的条目,所以后退button将我带回到我开始的地方。 虽然我觉得这是错误的,因为我不认为Angular应该自动删除这个历史logging对我来说,这实际上是我的特定用例所期望的行为。 
 问题是,如果我使用<a href="/page/?x=1">链接到页面,然后以相同的方式删除查询string,我在我的浏览器历史logging中获得额外的条目,所以我必须点击后退button两次才能返回到我开始的位置。 这是不一致的行为,但实际上这似乎更正确。 
 我可以通过使用$location.search('x', null).replace()来轻松解决href链接的问题,但是当你通过ui-sref链接登陆时, 。 
经过大量的摆弄之后,我就想到了这个问题:
 在我的应用程序的runfunction,我补充说: 
 $rootScope.$on('$locationChangeSuccess', function () { $rootScope.locationPath = $location.path(); }); 
然后,我使用这段代码删除查询string参数:
 $location.search('x', null); if ($location.path() === $rootScope.locationPath) { $location.replace(); } 
把位置哈希值设置为空值怎么样?
 $location.hash(null);