diff --git a/src/route-segment.js b/src/route-segment.js index 2aed975..32f7a51 100644 --- a/src/route-segment.js +++ b/src/route-segment.js @@ -126,7 +126,16 @@ mod.provider( '$routeSegment', segmentRoutes[name] = route; return this; }; - + + /** + * The shorthand for $routeProvider.otherwise() method with specified route. + * @param {string|function} route Route URL, e.g. '/'; or function that return a URL + */ + $routeSegmentProvider.otherwise = function(route) { + $routeProvider.otherwise({redirectTo: route}); + return this; + }; + // Extending the provider with the methods of rootPointer // to start configuration. angular.extend($routeSegmentProvider, rootPointer); @@ -409,7 +418,7 @@ mod.provider( '$routeSegment', params.watcher, {}, {segment: $routeSegment.chain[index]}); - } + }; var lastWatcherValue = getWatcherValue(); diff --git a/test/unit/route-segment.spec.js b/test/unit/route-segment.spec.js index 99f802c..eb0415b 100644 --- a/test/unit/route-segment.spec.js +++ b/test/unit/route-segment.spec.js @@ -170,6 +170,15 @@ describe('route segment', function() { }); + it('should redirect to / when invalid section', function () { + $routeSegmentProvider.otherwise('/1'); + + $location.path('/invalid'); + $rootScope.$broadcast('$locationChangeSuccess', $location.absUrl(), null); + $rootScope.$on('$locationChangeSuccess', function() { + expect($location.url()).toBe('/1'); + }); + }); it('should auto-fetch templateUrl by $http', function () {