防止AngularJS路由中的url编码

Tri*_*ong 9 angularjs

目前,当我将查询字符串传递给$ location的search()方法时,我的查询字符串是uri编码的

$location.path('/some_path').search({'ids[]': 1})
Run Code Online (Sandbox Code Playgroud)

http://some_url/some_path?ids%5B%5D=1
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法解决这个问题?

dim*_*irc 3

问题是 .search() 使用内部使用encodeURIComponent的encodeUriQuery,并且该函数转义除以下字符之外的所有字符:字母、十进制数字、 - _ 。!〜 * ' ( )

Angular源代码中的当前函数:

/**
 * 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' : '+'));
}
Run Code Online (Sandbox Code Playgroud)

如果该函数有额外的替换,那么括号将保持未编码状态:

replace(/%5B/gi, '[').
replace(/%5D/gi, ']').
Run Code Online (Sandbox Code Playgroud)