Angular ui-sref 编码参数 [英] Angular ui-sref encode parameter

查看:27
本文介绍了Angular ui-sref 编码参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路由器看起来像这样:

My router looks like that:

.state('project', {
        'url': '/project/*path',
        'controller': 'ProjectController',
        'templateUrl': '/pages/project.html',
    });

但是当我使用

ui-sref="project({path: mypath})"

with mypath=part1/part2 我希望它变成 /project/part1/part2 但我得到 /project/part1%252Fpart2.

with mypath=part1/part2 I expect the it to turn into /project/part1/part2 but instead i get /project/part1%252Fpart2.

如何让它在不编码的情况下传递参数?

How can I make it pass parameter without encoding it ?

推荐答案

此处描述,您需要为 URL 参数声明自定义变量类型,以便不编码斜杠.引用来自 github 的评论:

As it was described here, you need to declare custom variable type for URL param in order to have slashes not encoded. Quoting comment from github:

如果您真的不想为您编码斜杠,请使用正则表达式注册自定义类型并声明 item_id 为您的自定义类型,即 url:/{item_id:MyType}

If you really don't want the slash encoded for you, register a custom type with your regexp and declare item_id to be your custom type, i.e., url: /{item_id:MyType}

function valToString(val) { return val != null ? val.toString() : val; }
function valFromString(val) { return val != null ? val.toString() : val; }
function regexpMatches(val) { /*jshint validthis:true */ return this.pattern.test(val); }
$urlMatcherFactory.type("MyType", {
  encode: valToString,
  decode: valFromString,
  is: regexpMatches,
  pattern: /[^/]+\/[^/]+/
});

这篇关于Angular ui-sref 编码参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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