Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 4292612

Browse files
committed
feat(route): Add support to catch-all parameters in routes
This allows routeProvider to accept parameters that matches substrings even when they contain slashes if they are prefixed with an asterisk instead of a colon. For example, routes like edit/color/:color/largecode/*largecode will match with something like this http://appdomain.com/edit/color/brown/largecode/code/with/slashs.
1 parent 8ecfef7 commit 4292612

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/ng/route.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ function $RouteProvider(){
2121
* @param {string} path Route path (matched against `$location.path`). If `$location.path`
2222
* contains redundant trailing slash or is missing one, the route will still match and the
2323
* `$location.path` will be updated to add or drop the trailing slash to exacly match the
24-
* route definition.
24+
* route definition. A path can contain single parameters prefixed with a colon or catch-all
25+
* partials (multiple levels including slashes) if they are prefixed with an asterisk.
26+
*
2527
* @param {Object} route Mapping information to be assigned to `$route.current` on route
2628
* match.
2729
*

test/ng/routeSpec.js

+57
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,63 @@ describe('$route', function() {
5959
});
6060
});
6161

62+
it('should route and fire change event when catch-all params are used', function() {
63+
var log = '',
64+
lastRoute,
65+
nextRoute;
66+
67+
module(function($routeProvider) {
68+
$routeProvider.when('/Book1/:book/Chapter/:chapter/*highlight',
69+
{controller: noop, templateUrl: 'Chapter.html'});
70+
$routeProvider.when('/Book2/:book/*highlight/Chapter/:chapter',
71+
{controller: noop, templateUrl: 'Chapter.html'});
72+
$routeProvider.when('/Blank', {});
73+
});
74+
inject(function($route, $location, $rootScope) {
75+
$rootScope.$on('$routeChangeStart', function(event, next, current) {
76+
log += 'before();';
77+
expect(current).toBe($route.current);
78+
lastRoute = current;
79+
nextRoute = next;
80+
});
81+
$rootScope.$on('$routeChangeSuccess', function(event, current, last) {
82+
log += 'after();';
83+
expect(current).toBe($route.current);
84+
expect(lastRoute).toBe(last);
85+
expect(nextRoute).toBe(current);
86+
});
87+
88+
$location.path('/Book1/Moby/Chapter/Intro/one').search('p=123');
89+
$rootScope.$digest();
90+
$httpBackend.flush();
91+
expect(log).toEqual('before();after();');
92+
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one', p:'123'});
93+
94+
log = '';
95+
$location.path('/Blank').search('ignore');
96+
$rootScope.$digest();
97+
expect(log).toEqual('before();after();');
98+
expect($route.current.params).toEqual({ignore:true});
99+
100+
log = '';
101+
$location.path('/Book1/Moby/Chapter/Intro/one/two').search('p=123');
102+
$rootScope.$digest();
103+
expect(log).toEqual('before();after();');
104+
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one/two', p:'123'});
105+
106+
log = '';
107+
$location.path('/Book2/Moby/one/two/Chapter/Intro').search('p=123');
108+
$rootScope.$digest();
109+
expect(log).toEqual('before();after();');
110+
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one/two', p:'123'});
111+
112+
log = '';
113+
$location.path('/NONE');
114+
$rootScope.$digest();
115+
expect(log).toEqual('before();after();');
116+
expect($route.current).toEqual(null);
117+
});
118+
});
62119

63120
it('should not change route when location is canceled', function() {
64121
module(function($routeProvider) {

0 commit comments

Comments
 (0)