angularjs – Angular ui-sref编码参数

前端之家收集整理的这篇文章主要介绍了angularjs – Angular ui-sref编码参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的路由器看起来像这样:
  1. .state('project',{
  2. 'url': '/project/*path','controller': 'ProjectController','templateUrl': '/pages/project.html',});

但是当我用的时候

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

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

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

因为它是 described here,所以你需要为URL参数声明自定义变量类型,以便不对斜杠进行编码.引用github的评论

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}

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

猜你在找的Angularjs相关文章