Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

I ny view I have: <a ui-sref="app.file({path: file.path})">{{ file.path }}</a>

file.path is html/hero-sidebar.html

My state is defined as:

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

But when I click the link, it goes to http://localhost:9001/app/file/html%252Fhero-sidebar.html

What I want is for it to go to http://localhost:9001/app/file/html/hero-sidebar.html

Is this possible using ui-router with AngularJS?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is already logged issue in ui-router Github, you could found it here

In order to solve this you need to add this code inside your config which will take care of encoding and decoding of 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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...