MCPcopy Create free account
hub / github.com/angular-ui/ui-router / $HttpParamSerializerJQLikeProvider

Function $HttpParamSerializerJQLikeProvider

test/angular/1.6/angular.js:11366–11437  ·  view source on GitHub ↗

@this

()

Source from the content-addressed store, hash-verified

11364
11365/** @this */
11366function $HttpParamSerializerJQLikeProvider() {
11367 /**
11368 * @ngdoc service
11369 * @name $httpParamSerializerJQLike
11370 *
11371 * @description
11372 *
11373 * Alternative {@link $http `$http`} params serializer that follows
11374 * jQuery's [`param()`](http://api.jquery.com/jquery.param/) method logic.
11375 * The serializer will also sort the params alphabetically.
11376 *
11377 * To use it for serializing `$http` request parameters, set it as the `paramSerializer` property:
11378 *
11379 * ```js
11380 * $http({
11381 * url: myUrl,
11382 * method: 'GET',
11383 * params: myParams,
11384 * paramSerializer: '$httpParamSerializerJQLike'
11385 * });
11386 * ```
11387 *
11388 * It is also possible to set it as the default `paramSerializer` in the
11389 * {@link $httpProvider#defaults `$httpProvider`}.
11390 *
11391 * Additionally, you can inject the serializer and use it explicitly, for example to serialize
11392 * form data for submission:
11393 *
11394 * ```js
11395 * .controller(function($http, $httpParamSerializerJQLike) {
11396 * //...
11397 *
11398 * $http({
11399 * url: myUrl,
11400 * method: 'POST',
11401 * data: $httpParamSerializerJQLike(myData),
11402 * headers: {
11403 * 'Content-Type': 'application/x-www-form-urlencoded'
11404 * }
11405 * });
11406 *
11407 * });
11408 * ```
11409 *
11410 */
11411 this.$get = function() {
11412 return function jQueryLikeParamSerializer(params) {
11413 if (!params) return '';
11414 var parts = [];
11415 serialize(params, '', true);
11416 return parts.join('&');
11417
11418 function serialize(toSerialize, prefix, topLevel) {
11419 if (toSerialize === null || isUndefined(toSerialize)) return;
11420 if (isArray(toSerialize)) {
11421 forEach(toSerialize, function(value, index) {
11422 serialize(value, prefix + '[' + (isObject(value) ? index : '') + ']');
11423 });

Callers

nothing calls this directly

Calls 1

serializeFunction · 0.70

Tested by

no test coverage detected