如何将带有 URL 的数据传递给 ui-sref?

How can I pass data with URLs to ui-sref?

我的观点是:<a ui-sref="app.file({path: file.path})">{{ file.path }}</a>

file.pathhtml/hero-sidebar.html

我的状态定义为:

  .state('app.file', {
    url: '/file/*path'
    views:
      repositoryView:
        templateUrl: '/templates/app/_file.html'
  })

但是当我点击 link 时,它会转到 http://localhost:9001/app/file/html%252Fhero-sidebar.html

我想要的是http://localhost:9001/app/file/html/hero-sidebar.html

是否可以将 ui-路由器与 AngularJS 一起使用?

ui-router Github, you could found it here

中已经记录了问题

为了解决这个问题,您需要在您的配置中添加这段代码,它将处理 url.

的编码和解码

you could override the built-in string type (which is performing the slash encoding) by registering your own replacement "string" type using below code

代码

app.config(['$urlMatcherFactory', '$stateProvider', function($urlMatcherFactory, $stateProvider) {
    //$stateProvider.state() & other code here

    function valToString(val) {
        return val != null ? val.toString() : val;
    }

    function regexpMatches(val) { /*jshint validthis:true */
        return this.pattern.test(val);
    }
    $urlMatcherFactory.type("string", {
        encode: valToString,
        decode: valToString,
        is: regexpMatches,
        pattern: /[^/]*/
    });
}]);

Working Plunkr