我的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上的可见性中删除.
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)
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
小智 27
您可以使用以下命令删除特定查询参数:
delete $location.$$search.nameOfParameter;
Run Code Online (Sandbox Code Playgroud)
或者,您可以通过将搜索设置为空对象来清除所有查询参数:
$location.$$search = {};
Run Code Online (Sandbox Code Playgroud)
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.1用html5Mode(true).)
Bos*_*osh 12
html5mode= 时需要使它工作false?所有其他的答案只有工作的时候角的html5mode是true.如果你在外面工作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)
如果您想移动到另一个 URL 并清除查询参数,只需使用:
$location.path('/my/path').search({});
Run Code Online (Sandbox Code Playgroud)
只需使用
$location.url();
Run Code Online (Sandbox Code Playgroud)
代替
$location.path();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114595 次 |
| 最近记录: |