admin管理员组

文章数量:1323348

I wonder if this is possible - I need to match the following URLs with one pattern:

/one/app/home
/one/two/app/home
/one/two/three/app/home
...

I understand that AngularJS routing doesn't support regex, but Angular UI Router does.

main.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
    $urlRouterProvider.otherwise("/fail");
    $stateProvider
    .state("home", {
        url: "{.+}/app/home",
        templateUrl: "assets/tpl/home.html",
        controller: "homeController"
    })
    $locationProvider.html5Mode(true);
});

This doesn't work though (goes to /fail with all the examples). Is what I want to do possible at all?

I wonder if this is possible - I need to match the following URLs with one pattern:

/one/app/home
/one/two/app/home
/one/two/three/app/home
...

I understand that AngularJS routing doesn't support regex, but Angular UI Router does.

main.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
    $urlRouterProvider.otherwise("/fail");
    $stateProvider
    .state("home", {
        url: "{.+}/app/home",
        templateUrl: "assets/tpl/home.html",
        controller: "homeController"
    })
    $locationProvider.html5Mode(true);
});

This doesn't work though (goes to /fail with all the examples). Is what I want to do possible at all?

Share Improve this question asked Apr 17, 2014 at 11:31 CaballeroCaballero 12.1k22 gold badges105 silver badges169 bronze badges 3
  • Use .+ instead of {.+}, and add a $ at the end to ensure the app/home part is the last one: .+/app/home$ – Robin Commented Apr 17, 2014 at 11:34
  • @Robin I think there are certain limitations of URL parsing here not allowing this to work. – Caballero Commented Apr 17, 2014 at 11:38
  • Sorry, I wasn't familiar with Angular UI regex syntax... As a random thought I'd say it doesn't work either because the "name" part is mandatory ({id:...}), or because the regex can't include a / (ie it's only applied between to slashes). But I'm not familiar with the specifics, so this is just a guess in the wild. Good luck! – Robin Commented Apr 17, 2014 at 11:45
Add a ment  | 

1 Answer 1

Reset to default 6

You should use it like this:

state("home", {
    url: "/{beginPath:.+}{endPath:/app/home}",
    templateUrl: "assets/tpl/home.html",
    controller: "homeController"
});

Then you you will have access to $stateParams.beginPath (which is equal to evertyhing before /app/home/) and to $stateParams.endPath which will be equal to /app/home/

If /one/ is necessary in the beggining then use something like this:

url: "/{beginPath:one/.+}{endPath:/app/home}"

本文标签: javascriptAngularJS Ui Router regex matching wildcard in urlStack Overflow