admin管理员组

文章数量:1315338

I came across an Angular.js service named $httpParamSerializer and thought it could be useful for my code. However, when I tried to inject it Angular.js didn't recognize it and threw an "unknown provider" error.

Isn't $httpParamSerializer a built-in service (just like $http)? Why is this happening?

I have no problem injecting other built-in services such as $http, $httpBackend etc.

Thanks.

I came across an Angular.js service named $httpParamSerializer and thought it could be useful for my code. However, when I tried to inject it Angular.js didn't recognize it and threw an "unknown provider" error.

Isn't $httpParamSerializer a built-in service (just like $http)? Why is this happening?

I have no problem injecting other built-in services such as $http, $httpBackend etc.

Thanks.

Share Improve this question asked Apr 11, 2015 at 1:23 melonccolimelonccoli 4081 gold badge5 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

It was just recently added in Angular v1.4.0-rc.0. - so, check and fix your version of Angular:

<script src="https://code.angularjs/1.4.0-rc.0/angular.js">

I needed this also, but at the moment we cannot upgrade, so I just took code and created provider and added it to my "mon" module. Once we upgrade, I'll just remove it.

(function (angular) {

    'use strict';

    var serviceId = '$httpParamSerializer';

    var mon = angular.module('mon');

    mon.provider(serviceId, $HttpParamSerializerProvider);


    function $HttpParamSerializerProvider() {

        function sortedKeys(obj) {
            return Object.keys(obj).sort();
        }

        function forEachSorted(obj, iterator, context) {
            var keys = sortedKeys(obj);
            for (var i = 0; i < keys.length; i++) {
                iterator.call(context, obj[keys[i]], keys[i]);
            }
            return keys;
        }

        function encodeUriQuery(val, pctEncodeSpaces) {
            return encodeURIComponent(val).
                       replace(/%40/gi, '@').
                       replace(/%3A/gi, ':').
                       replace(/%24/g, '$').
                       replace(/%2C/gi, ',').
                       replace(/%3B/gi, ';').
                       replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
        }

        function serializeValue(v) {
            if (isObject(v)) {
                return isDate(v) ? v.toISOString() : toJson(v);
            }
            return v;
        }

        function isUndefined(value) { return typeof value === 'undefined'; }

        var isArray = Array.isArray;

        function isObject(value) {
            // http://jsperf./isobject4
            return value !== null && typeof value === 'object';
        }

        /**
     * @ngdoc service
     * @name $httpParamSerializer
     * @description
     *
     * Default {@link $http `$http`} params serializer that converts objects to strings
     * according to the following rules:
     *
     * * `{'foo': 'bar'}` results in `foo=bar`
     * * `{'foo': Date.now()}` results in `foo=2015-04-01T09%3A50%3A49.262Z` (`toISOString()` and encoded representation of a Date object)
     * * `{'foo': ['bar', 'baz']}` results in `foo=bar&foo=baz` (repeated key for each array element)
     * * `{'foo': {'bar':'baz'}}` results in `foo=%7B%22bar%22%3A%22baz%22%7D"` (stringified and encoded representation of an object)
     *
     * Note that serializer will sort the request parameters alphabetically.
     * */

        this.$get = function() {
            return function ngParamSerializer(params) {
                if (!params) return '';
                var parts = [];
                forEachSorted(params, function(value, key) {
                    if (value === null || isUndefined(value)) return;
                    if (isArray(value)) {
                        forEach(value, function(v, k) {
                            parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(v)));
                        });
                    } else {
                        parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(value)));
                    }
                });

                return parts.join('&');
            };
        };




    }
}(angular))

本文标签: javascriptUnknown provider for httpParamSerializerStack Overflow