如何以编程方式创建AngularJS网址 [英] How to programatically create URLs with AngularJS

查看:108
本文介绍了如何以编程方式创建AngularJS网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我与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开始,你可以使用 $ httpParamSerializer 为:

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网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆