Build query string from parameters object

前端 未结 4 1199
不知归路
不知归路 2020-12-05 03:54

How to build a url with query parameters in Angularjs.

I see the API $location.search()

the problem is $location(url) is to redirect to the url. In my case,

4条回答
  •  遥遥无期
    2020-12-05 04:27

    There is a nice solution as of 1.4+. You can build a query string from a parameters object with $httpParamSerializer :

    var qs = $httpParamSerializer(params);
    

    See docs here

    Default $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.
    

提交回复
热议问题