Matching url path with regex in $urlRouterProvider.when()

两盒软妹~` 提交于 2020-01-01 06:53:08

问题


Based on the angular ui-router wiki (https://github.com/angular-ui/ui-router/wiki/URL-Routing#urlrouterprovider) it should be possible to use regex for matching the incoming path. How could I express regex to match following redirection rules:

$urlRouterProvider
  .when('', '/events')
  .when('/', '/events')
  .when('/:eventName', '/events/:eventName')
  .when('/results', '/events/results')

Test example:

localhost => localhost/#/events
localhost/ => localhost/#/events
localhost/myEvent => localhost/#/events/myEvent
localhost/results => localhost/#/events/results
localhost/#/events => localhost/#/events
localhost/#/results => localhost/#/results

回答1:


I found the answer. Order of the when() statements and the state definition is important.

This works:

$urlRouterProvider
    .when('', '/events')
    .when('/', '/events')
    .when('/results', '/events/results')
$stateProvider
    .state('events', {
    ......
    }
$urlRouterProvider // This needs to be at the end to match all other matches
    .when('/:eventName', '/events/:eventName')


来源:https://stackoverflow.com/questions/23094289/matching-url-path-with-regex-in-urlrouterprovider-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!