问题描述
目前,我与AngularJS框架玩弄。我使用的是$航线的深层链接到我的单页的应用程序。
Currently I am toying with the AngularJS framework. I'm using the $route service for deep linking into my single-page application.
现在,我想我的应用程序内导航,比如说,通过改变只是当前URL的搜索部分。这是很容易在JavaScript中使用$位置服务做的,但我怎么能构建从目前的路线href属性只搜索部分取代?
Now I would like to navigate inside my application, say, by changing just the search part of the current URL. It is easy to do using the $location service in JavaScript, but how can I construct an href attribute from the current route with just the search part replaced?
我必须做手工它还是有它的AngularJS的方式?
Do I have to do it by hand or is there an AngularJS way for it?
的补充:的
当然,$位置服务拥有所有的方法来计算此类URL。但我不能使用它,因为$的位置也没有导航的浏览器窗口,以新的URL。
Of course, the $location service has all methods to calculate such URLs. But I cannot use it because $location does also navigate the browser window to the new URL.
有与手工创建网址的另一个并发症:一是必须检查遗留#-method或新的历史API是否使用。根据这一点,URL具有以包括#-sign或不
There is another complication with creating URLs by hand: One has to check whether the legacy #-method or the new History API is used. Depending on this, the URL has to include a #-sign or not.
推荐答案
从V1.4开始,你可以使用为:
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;
});
用法
要制作的http:// URL参数1 =值1&放大器;参数2 = value2_1&放大器;参数2 = value2_2
与调用它:
urlBuilder('http://url', { param1: 'value1', param2: ['value2_1', 'value2_2'] });
这篇关于如何以编程方式创建AngularJS网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!