Skip to content

Commit 3b5d75c

Browse files
Erin Altenhof-Longbtford
authored andcommitted
feat(ngRoute): alias string as redirectTo property in .otherwise()
Allow `.otherwise()` to interpret a string parameter as the `redirectTo` property Closes angular#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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 17 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy