以$ location.search():如何从URL中移除一个参数时,它的空 [英] with $location.search(): how to remove a parameter from the url when it's null
问题描述
我使用 $ location.search()
设置一个网址( http://docs.angularjs.org/api/ng 。$#位置搜索)
被传递到搜索的搜索参数()
被设置形式。选择和取消选择的元素会导致一些参数值是空等的URL看起来是这样的:
myapp.com/search?type=text¶meter=null
所以我想从URL中移除那些空的参数。在文档中,有一个paramValue,它可以作为第二个参数传递给.search(搜索,paramValue)的如果值为空
中,参数将被删除。的
但我不能使这项工作...任何建议?
编辑:这里是基于@BKM解释的解决方案
删除各项参数这是空
,有必要遍历所有的人,并测试每一个,就像这样:
为(VAR我在搜索){
如果$ location.search(I,空)(搜索[I]!);
}
$ location.search(搜索);
使用 $位置
服务,您可以通过分配一个空值删除搜索参数:
在这种情况下,当你的参数为空,你的情况参数
您可以在URL中使用分配给它一个空值删除等;
$ location.search('参数',NULL);
希望它帮助。
I am using $location.search()
to set an url (http://docs.angularjs.org/api/ng.$location#search)
the search parameter which is passed to the search()
is set by a form. selecting and deselecting an element causes some parameters value to be "null" and so the url looks like this :
myapp.com/search?type=text¶meter=null
so i would like to remove those "null" parameters from the url. In the documentation, there is a "paramValue" which can be passed as a second parameters to .search(search, paramValue) : If the value is null
, the parameter will be deleted.
but i can't make this work… any suggestion ?
edit: here is a solution based on @BKM explanation
to remove every parameters which are null
, it's necessary to loop through all of them and test each one, like this :
for (var i in search) {
if (!search[i]) $location.search(i, null);
}
$location.search(search);
Using the $location
service, you can remove the search param by assigning it a null value:
In the case when your parameter is null, in your case 'parameter'
you can remove it from the url by assigning it a null value like;
$location.search('parameter', null);
Hope it helps.
这篇关于以$ location.search():如何从URL中移除一个参数时,它的空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!