问题
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