Skip to content

Commit

Permalink
feat($route): when matching consider trailing slash as optional
Browse files Browse the repository at this point in the history
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
  • Loading branch information
IgorMinar committed Mar 20, 2012
1 parent dc8b121 commit eb5e3a1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/service/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function $RouteProvider(){
function switchRouteMatcher(on, when) {
// TODO(i): this code is convoluted and inefficient, we should construct the route matching
// regex only once and then reuse it
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1").replace(/\/$/, '') + '/?$',
params = [],
dst = {};
forEach(when.split(/\W/), function(param) {
Expand Down
26 changes: 26 additions & 0 deletions test/service/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,32 @@ describe('$route', function() {
});


it('should match route with and without trailing slash', function() {
module(function($routeProvider){
$routeProvider.when('/foo', {template: 'foo.html'});
$routeProvider.when('/bar/', {template: 'bar.html'});
});

inject(function($route, $location, $rootScope) {
$location.path('/foo');
$rootScope.$digest();
expect($route.current.template).toBe('foo.html');

$location.path('/foo/');
$rootScope.$digest();
expect($route.current.template).toBe('foo.html');

$location.path('/bar');
$rootScope.$digest();
expect($route.current.template).toBe('bar.html');

$location.path('/bar/');
$rootScope.$digest();
expect($route.current.template).toBe('bar.html');
});
});


describe('redirection', function() {
it('should support redirection via redirectTo property by updating $location', function() {
module(function($routeProvider) {
Expand Down

0 comments on commit eb5e3a1

Please sign in to comment.