Skip to content

Commit d89bacd

Browse files
author
Erin Altenhof-Long
committed
feat(ngRoute): alias string as redirectTo property in .otherwise()
Allow .otherwise() to interpret a string parameter as the redirectTo property to handle the common misinterpretation that .otherwise() takes as a parameter the string to be matched in the otherwise case. Closes angular#7794
1 parent 0dbec22 commit d89bacd

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/ngRoute/route.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ function $RouteProvider(){
158158
pathRegExp(redirectPath, route)
159159
);
160160
}
161-
162161
return this;
163162
};
164163

@@ -211,10 +210,14 @@ function $RouteProvider(){
211210
* Sets route definition that will be used on route change when no other route definition
212211
* is matched.
213212
*
214-
* @param {Object} params Mapping information to be assigned to `$route.current`.
213+
* @param {Object} params Mapping information to be assigned to `$route.current`. If of type
214+
* string, the value is assumed to map to `redirectTo`.
215215
* @returns {Object} self
216216
*/
217217
this.otherwise = function(params) {
218+
if(typeof params === 'string') {
219+
params = {redirectTo: params};
220+
}
218221
this.when(null, params);
219222
return this;
220223
};

test/ngRoute/routeSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,23 @@ describe('$route', function() {
460460
expect(onChangeSpy).toHaveBeenCalled();
461461
});
462462
});
463+
464+
465+
it('should interpret a string as a redirect route', function() {
466+
module(function($routeProvider) {
467+
$routeProvider.when('/foo', {templateUrl: 'foo.html'});
468+
$routeProvider.when('/baz', {templateUrl: 'baz.html'});
469+
$routeProvider.otherwise('/foo');
470+
});
471+
472+
inject(function($route, $location, $rootScope) {
473+
$location.path('/unknownRoute');
474+
$rootScope.$digest();
475+
476+
expect($location.path()).toBe('/foo');
477+
expect($route.current.templateUrl).toBe('foo.html');
478+
});
479+
});
463480
});
464481

465482

0 commit comments

Comments
 (0)