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

Fix 592/1699 prevent route change start #2555

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ function $RouteProvider(){
* defined in `resolve` route property. Once all of the dependencies are resolved
* `$routeChangeSuccess` is fired.
*
* Call event.preventDefault() to prevent changing route but leave the browser Url
* to it's new location (as opposed to calling event.preventDefault() on $location's
* $locationChangeStart event which cancels changing the browser's Url).
*
*
* @param {Route} next Future route information.
* @param {Route} current Current route information.
*/
Expand Down Expand Up @@ -407,7 +412,9 @@ function $RouteProvider(){
$rootScope.$broadcast('$routeUpdate', last);
} else if (next || last) {
forceReload = false;
$rootScope.$broadcast('$routeChangeStart', next, last);
if ( $rootScope.$broadcast('$routeChangeStart', next, last).defaultPrevented ){
return;
}
$route.current = next;
if (next) {
if (next.redirectTo) {
Expand Down
22 changes: 22 additions & 0 deletions test/ng/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ describe('$route', function() {
});


it('should not change route when $routeChangeStart is canceled', function() {
module(function($routeProvider) {
$routeProvider.when('/somePath', {template: 'some path'});
});
inject(function($route, $location, $rootScope, $log) {
$rootScope.$on('$routeChangeStart', function(event) {
$log.info('$routeChangeStart');
event.preventDefault();
});

$rootScope.$on('$routeChangeSuccess', function(event) {
throw new Error('Should not get here');
});

$location.path('/somePath');
$rootScope.$digest();

expect($log.info.logs.shift()).toEqual(['$routeChangeStart']);
});
});


describe('should match a route that contains special chars in the path', function() {
beforeEach(module(function($routeProvider) {
$routeProvider.when('/$test.23/foo*(bar)/:baz', {templateUrl: 'test.html'});
Expand Down