一尘不染

角度ui-sref编码参数

angularjs

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

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

但是当我使用

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

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

如何使它通过参数而不进行编码?


阅读 220

收藏
2020-07-04

共1个答案

一尘不染

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:

如果您真的不希望为斜杠编码,请在regexp中注册一个自定义类型,然后声明item_id为您的自定义类型,即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: /[^/]+\/[^/]+/
});
2020-07-04