feat(ngRoute): alias string as redirectTo property in .otherwise()

Allow `.otherwise()` to interpret a string parameter
as the `redirectTo` property

Closes #7794
This commit is contained in:
Erin Altenhof-Long
2014-06-12 12:01:29 -07:00
committed by Brian Ford
parent 719c747cd8
commit 3b5d75c021
2 changed files with 22 additions and 1 deletions

View File

@@ -212,10 +212,14 @@ function $RouteProvider(){
* Sets route definition that will be used on route change when no other route definition
* is matched.
*
* @param {Object} params Mapping information to be assigned to `$route.current`.
* @param {Object|string} params Mapping information to be assigned to `$route.current`.
* If called with a string, the value maps to `redirectTo`.
* @returns {Object} self
*/
this.otherwise = function(params) {
if (typeof params === 'string') {
params = {redirectTo: params};
}
this.when(null, params);
return this;
};

View File

@@ -460,6 +460,23 @@ describe('$route', function() {
expect(onChangeSpy).toHaveBeenCalled();
});
});
it('should interpret a string as a redirect route', function() {
module(function($routeProvider) {
$routeProvider.when('/foo', {templateUrl: 'foo.html'});
$routeProvider.when('/baz', {templateUrl: 'baz.html'});
$routeProvider.otherwise('/foo');
});
inject(function($route, $location, $rootScope) {
$location.path('/unknownRoute');
$rootScope.$digest();
expect($location.path()).toBe('/foo');
expect($route.current.templateUrl).toBe('foo.html');
});
});
});