mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-06-11 15:52:50 +08:00
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:
committed by
Brian Ford
parent
719c747cd8
commit
3b5d75c021
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user