I notice that when I passed a parameter that is an array to $location.search(), it was encoded as in the following example
我注意到当我将一个数组参数传递给$ location.search()时,它被编码为如下例所示
$location.path('/somePath').search('ids[]=', [1, 2, 3]);
$ location.path('/ somePath')。search('ids [] =',[1,2,3]);
becomes
/somePath?ds%5B%5D=1&ds%5B%5D=2&ds%5B%5D=3
Is there a way to avoid the url encoding?
有没有办法避免网址编码?
2 个解决方案
#1
2
Excuse the late reply, but I struck a similar predicament myself and thought I'd offer advice.
请原谅迟到的答复,但我自己也遇到了类似的困境,并认为我会提供建议。
In short, if you intend on using $location.search
you cannot avoid the URL being encoded.
简而言之,如果您打算使用$ location.search,则无法避免编码的URL。
If you take a look at the Angular source, location.js
specifically, you'll notice that the functions return composed URLs (i.e. LocationHtml5Url
) all rely on another function call named toKeyValue
, which will encode all key-value pairs whenever they are set.
如果你专门看一下Angular源,location.js,你会注意到函数返回组合的URL(即LocationHtml5Url)都依赖于另一个名为toKeyValue的函数调用,它将在设置时对所有键值对进行编码。 。
Furthermore, in the example use case you've provided, you don't need the equals inside your key. When $location.search
is called with two parameters, they are treated as a key-value pair by Angular.
此外,在您提供的示例用例中,您不需要键内的等号。当使用两个参数调用$ location.search时,它们被Angular视为键值对。
If you need to make use of the location URL after it has been encoded, you could always call decodeURIComponent
.
如果您需要在编码后使用位置URL,则可以随时调用decodeURIComponent。
#2
1
Some years late, but I have found a way to bypass the URI encode made by the $location service: just rewrite the window.encodeURIComponent
几年后,但我找到了绕过$ location服务所做的URI编码的方法:只需重写window.encodeURIComponent
var original = window.encodeURIComponent;
window.encodeURIComponent = function (str) {
return decodeURI(str);
};
$location.url('/somePath?ids=[1,2,3]');
window.encodeURIComponent = original;
It is not the best solution, but it works as intended.
它不是最好的解决方案,但它按预期工作。
#1
2
Excuse the late reply, but I struck a similar predicament myself and thought I'd offer advice.
请原谅迟到的答复,但我自己也遇到了类似的困境,并认为我会提供建议。
In short, if you intend on using $location.search
you cannot avoid the URL being encoded.
简而言之,如果您打算使用$ location.search,则无法避免编码的URL。
If you take a look at the Angular source, location.js
specifically, you'll notice that the functions return composed URLs (i.e. LocationHtml5Url
) all rely on another function call named toKeyValue
, which will encode all key-value pairs whenever they are set.
如果你专门看一下Angular源,location.js,你会注意到函数返回组合的URL(即LocationHtml5Url)都依赖于另一个名为toKeyValue的函数调用,它将在设置时对所有键值对进行编码。 。
Furthermore, in the example use case you've provided, you don't need the equals inside your key. When $location.search
is called with two parameters, they are treated as a key-value pair by Angular.
此外,在您提供的示例用例中,您不需要键内的等号。当使用两个参数调用$ location.search时,它们被Angular视为键值对。
If you need to make use of the location URL after it has been encoded, you could always call decodeURIComponent
.
如果您需要在编码后使用位置URL,则可以随时调用decodeURIComponent。
#2
1
Some years late, but I have found a way to bypass the URI encode made by the $location service: just rewrite the window.encodeURIComponent
几年后,但我找到了绕过$ location服务所做的URI编码的方法:只需重写window.encodeURIComponent
var original = window.encodeURIComponent;
window.encodeURIComponent = function (str) {
return decodeURI(str);
};
$location.url('/somePath?ids=[1,2,3]');
window.encodeURIComponent = original;
It is not the best solution, but it works as intended.
它不是最好的解决方案,但它按预期工作。