The problem is that .search() uses encodeUriQuery that internally uses encodeURIComponent and this function escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )
The current function inside Angular's source code:
/**
* This method is intended for encoding *key* or *value* parts of query component. We need a custom
* method because encodeURIComponent is too aggressive and encodes stuff that doesn't have to be
* encoded per http://tools.ietf.org/html/rfc3986:
* query = *( pchar / "/" / "?" )
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* pct-encoded = "%" HEXDIG HEXDIG
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
function encodeUriQuery(val, pctEncodeSpaces) {
return encodeURIComponent(val).
replace(/%40/gi, '@').
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
}
If that function had this additional replaces, then the brackets will keep unencoded:
replace(/%5B/gi, '[').
replace(/%5D/gi, ']').
$location.search()is called it un-encodes the URL back into your original object. At this point, the only reason for doing what you're trying to do is to make the URL look prettier in the browser.