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

Commit 3b5d75c

Browse files
Erin Altenhof-Longbtford
Erin Altenhof-Long
authored andcommitted
feat(ngRoute): alias string as redirectTo property in .otherwise()
Allow `.otherwise()` to interpret a string parameter as the `redirectTo` property Closes #7794
1 parent 719c747 commit 3b5d75c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/ngRoute/route.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,14 @@ function $RouteProvider(){
212212
* Sets route definition that will be used on route change when no other route definition
213213
* is matched.
214214
*
215-
* @param {Object} params Mapping information to be assigned to `$route.current`.
215+
* @param {Object|string} params Mapping information to be assigned to `$route.current`.
216+
* If called with a string, the value maps to `redirectTo`.
216217
* @returns {Object} self
217218
*/
218219
this.otherwise = function(params) {
220+
if (typeof params === 'string') {
221+
params = {redirectTo: params};
222+
}
219223
this.when(null, params);
220224
return this;
221225
};

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)