How to programmatically create URLs with AngularJS

后端 未结 3 1350
旧时难觅i
旧时难觅i 2021-02-02 08:14

Currently I am toying with the AngularJS framework. I\'m using the $route service for deep linking into my single-page application.

Now I would like to navigate inside m

3条回答
  •  抹茶落季
    2021-02-02 08:28

    Starting from v1.4 you can use $httpParamSerializer for that:

    angular.module('util').factory('urlBuilder', function($httpParamSerializer) {
        function buildUrl(url, params) {
            var serializedParams = $httpParamSerializer(params);
    
            if (serializedParams.length > 0) {
                url += ((url.indexOf('?') === -1) ? '?' : '&') + serializedParams;
            }
    
            return url;
        }
    
        return buildUrl;
    });
    

    Usage:

    To produce http://url?param1=value1¶m2=value2_1¶m2=value2_2 call it with:

    urlBuilder('http://url', { param1: 'value1', param2: ['value2_1', 'value2_2'] });
    

提交回复
热议问题