This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +44
-1
lines changed
2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,10 @@ function $RouteProvider(){
18
18
* @name angular.module.ng.$routeProvider#when
19
19
* @methodOf angular.module.ng.$routeProvider
20
20
*
21
- * @param {string } path Route path (matched against `$location.hash`)
21
+ * @param {string } path Route path (matched against `$location.path`). If `$location.path`
22
+ * contains redudant trailing slash or is missing one, the route will still match and the
23
+ * `$location.path` will be updated to add or drop the trailing slash to exacly match the
24
+ * route definition.
22
25
* @param {Object } route Mapping information to be assigned to `$route.current` on route
23
26
* match.
24
27
*
@@ -57,6 +60,16 @@ function $RouteProvider(){
57
60
var routeDef = routes [ path ] ;
58
61
if ( ! routeDef ) routeDef = routes [ path ] = { reloadOnSearch : true } ;
59
62
if ( route ) extend ( routeDef , route ) ; // TODO(im): what the heck? merge two route definitions?
63
+
64
+ // create redirection for trailing slashes
65
+ if ( path ) {
66
+ var redirectPath = ( path [ path . length - 1 ] == '/' )
67
+ ? path . substr ( 0 , path . length - 1 )
68
+ : path + '/' ;
69
+
70
+ routes [ redirectPath ] = { redirectTo : path } ;
71
+ }
72
+
60
73
return routeDef ;
61
74
} ;
62
75
Original file line number Diff line number Diff line change @@ -168,6 +168,36 @@ describe('$route', function() {
168
168
} ) ;
169
169
170
170
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 ( $location . path ( ) ) . toBe ( '/foo' ) ;
181
+ expect ( $route . current . template ) . toBe ( 'foo.html' ) ;
182
+
183
+ $location . path ( '/foo/' ) ;
184
+ $rootScope . $digest ( ) ;
185
+ expect ( $location . path ( ) ) . toBe ( '/foo' ) ;
186
+ expect ( $route . current . template ) . toBe ( 'foo.html' ) ;
187
+
188
+ $location . path ( '/bar' ) ;
189
+ $rootScope . $digest ( ) ;
190
+ expect ( $location . path ( ) ) . toBe ( '/bar/' ) ;
191
+ expect ( $route . current . template ) . toBe ( 'bar.html' ) ;
192
+
193
+ $location . path ( '/bar/' ) ;
194
+ $rootScope . $digest ( ) ;
195
+ expect ( $location . path ( ) ) . toBe ( '/bar/' ) ;
196
+ expect ( $route . current . template ) . toBe ( 'bar.html' ) ;
197
+ } ) ;
198
+ } ) ;
199
+
200
+
171
201
describe ( 'redirection' , function ( ) {
172
202
it ( 'should support redirection via redirectTo property by updating $location' , function ( ) {
173
203
module ( function ( $routeProvider ) {
You can’t perform that action at this time.
0 commit comments