次のように宣言された角度リソースがあります
angular.module('xpto', ['ngResource'])
.factory('XPTO', function ($resource, $location) {
var XPTO = $resource($location.protocol() + '://' + $location.host() +
':port' + '/myservice.svc/gerencia?sigla=:sigla',
{
port: ':' + $location.port()
}
);
return XPTO;
})
次のようなアンパサンド (&) を含むパラメーターを渡すサービスを呼び出したいと思います。
XPTO.query({ sigla: 'abc&d' }, function (gerencias) {
$scope.gerenciasBuscadas = gerencias;
});
ただし、AngularJS は & を適切にエンコードしません。"sigla=abc%26d" の代わりに "sigla=abc&d" を送信するため、サーバーは "sigla" のクエリ文字列パラメータ値を "abc&d" ではなく "abc" として認識します。
angular-resource-1.0.7.js を調べると、次のことがわかりました。
/**
* We need our custom method because encodeURIComponent is too aggressive and doesn't follow
* http://www.ietf.org/rfc/rfc3986.txt with regards to the character set (pchar) allowed in path
* segments:
* segment = *pchar
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
* pct-encoded = "%" HEXDIG HEXDIG
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
* / "*" / "+" / "," / ";" / "="
*/
function encodeUriSegment(val) {
return encodeUriQuery(val, true).
replace(/%26/gi, '&').
replace(/%3D/gi, '=').
replace(/%2B/gi, '+');
}
したがって、サーバーにリクエストを送信する前に、「&」をエンコードしてデコードします。サーバーに送信される直前に URL をカスタマイズできるポイントはありますか? encodeUriSegment を変更することはオプションですが、Angular 内の他のものを壊す可能性があります。何か案は?