2014-01-20 45 views
5

我注意到,当我通过一个参数,该参数是一个数组为$ location.search(),它被编码为在下面的示例中

$ location.path('/ somePath')。search('ids [] =',[1,2,3]);

变得

/somePath?DS%5B%5D = 1个& DS%5B%5D = 2个& DS%5B%5D = 3

有一种方法,以避免URL编码?

回答

2

请原谅迟到的回复,但我自己也遇到过类似的困境,并认为我会提供建议。

总之,如果您打算使用$location.search,则无法避免编码的URL。

如果你看一看的角源,location.js具体而言,你会发现,函数返回组成的URL(即LocationHtml5Url)都依赖于一个名为toKeyValue另一个函数调用,这将编码所有键值对,每当他们被设置。

此外,在您提供的示例用例中,您不需要密钥中的等号。当使用两个参数调用$location.search时,它们被Angular视为键值对。

如果您需要在编码后使用位置URL,则可以始终致电decodeURIComponent

1

几年晚了,但我已经找到一种方法来绕过由$位置服务进行URI编码:刚刚改写window.encodeURIComponent

var original = window.encodeURIComponent; 
window.encodeURIComponent = function (str) { 
    return decodeURI(str); 
}; 
$location.url('/somePath?ids=[1,2,3]'); 
window.encodeURIComponent = original; 

这是不是最好的解决方案,但它按预期工作。