$ httpParamSerializer的未知提供程序

时间:2015-04-11 01:23:25

标签: javascript angularjs

我遇到了一个名为$httpParamSerializer的Angular.js服务,并认为它对我的代码很有用。然而,当我试图注入它时,Angular.js并没有认识到它并且扔了一个未知的提供者"错误。

不是$httpParamSerializer内置服务(就像$http)?为什么会这样?

我可以注入其他内置服务,例如$http$httpBackend等。

感谢。

2 个答案:

答案 0 :(得分:5)

最近刚刚添加到Angular v1.4.0-rc.0. - 所以,请检查并修复您的Angular版本:

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

答案 1 :(得分:2)

我也需要这个,但目前我们无法升级,所以我只是接受了代码并创建了提供程序并将其添加到我的&#34; common&#34;模块。升级后,我会将其删除。

(function (angular) {

    'use strict';

    var serviceId = '$httpParamSerializer';

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

    common.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.com/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))