Skip to content

Commit eb5e3a1

Browse files
committed
feat($route): when matching consider trailing slash as optional
This makes for a much more flexible route matching: - route /foo matches /foo as well as /foo/ - route /bar/ matches /bar/ as well as /bar Closes angular#784
1 parent dc8b121 commit eb5e3a1

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/service/route.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function $RouteProvider(){
246246
function switchRouteMatcher(on, when) {
247247
// TODO(i): this code is convoluted and inefficient, we should construct the route matching
248248
// regex only once and then reuse it
249-
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
249+
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1").replace(/\/$/, '') + '/?$',
250250
params = [],
251251
dst = {};
252252
forEach(when.split(/\W/), function(param) {

test/service/routeSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,32 @@ describe('$route', function() {
168168
});
169169

170170

171+
it('should match route with and without trailing slash', function() {
172+
module(function($routeProvider){
173+
$routeProvider.when('/foo', {template: 'foo.html'});
174+
$routeProvider.when('/bar/', {template: 'bar.html'});
175+
});
176+
177+
inject(function($route, $location, $rootScope) {
178+
$location.path('/foo');
179+
$rootScope.$digest();
180+
expect($route.current.template).toBe('foo.html');
181+
182+
$location.path('/foo/');
183+
$rootScope.$digest();
184+
expect($route.current.template).toBe('foo.html');
185+
186+
$location.path('/bar');
187+
$rootScope.$digest();
188+
expect($route.current.template).toBe('bar.html');
189+
190+
$location.path('/bar/');
191+
$rootScope.$digest();
192+
expect($route.current.template).toBe('bar.html');
193+
});
194+
});
195+
196+
171197
describe('redirection', function() {
172198
it('should support redirection via redirectTo property by updating $location', function() {
173199
module(function($routeProvider) {

0 commit comments

Comments
 (0)