Angularjs:建立与查询字符串的URL [英] Angularjs: build url with query string
问题描述
如何建立与查询参数的URL中Angularjs。
我看到了API $ location.search()
问题是$位置(URL)是重定向到的URL。在我的情况,我想通过一个URL,键:用于查询参数值对,并建立了网址。像
网址: / A / B / C
params:一个 {字段1:值1,场2:值2}
结果:<?code> / A / B / C字段1 =值1&放大器;场2 =值2
我喜欢使用这个网址链接。我也看到角连接code
,&放大器;
字符。我能避免这种情况?
编辑:
我的目的是使用url作为HREF锚元素。我使用$ HTTP发送请求,但有时我需要提供一个链接,以查询参数(基于对当前对象)
感谢
有一个很好的解决方案为1.4+。你可以建立一个查询字符串从参数与对象 $ httpParamSerializer
:
VAR QS = $ httpParamSerializer(PARAMS);
默认$ HTTP PARAMS序列化程序将对象转换为字符串
根据以下规则:
块引用>{'富':'棒'}的结果富=酒吧
{'富':Date.now()}导致富= 2015-04-01T09%3A50%3A49.262Z(toISOString()和EN codeD重新$ P $ Date对象psentation)
{'富':'棒','巴兹']}的结果富=酒吧和放大器;富=巴兹(每个数组元素重复键)
{'富':{'酒吧':'巴兹'}}导致富=%7B%22bar%22%3A%22baz%22%7D(字符串化和连接codeD重新$ P $对象的psentation )
需要注意的是序列化器将请求参数按字母顺序排序。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, I want to pass a url and key:value pairs for the query params and build the url. something like
url:
/a/b/c
params:{field1: value1, field2: value2}
result:
/a/b/c?field1=value1&field2=value2
I like to use this url for links. I have also seen angular encode
?
,&
characters. Can I avoid this?Edit:
My intention was to use the url as href for anchor elements. I do use $http to send the request, but sometimes I need to provide a link, with query params (based on the current object)
Thanks
解决方案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);
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.
这篇关于Angularjs:建立与查询字符串的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!