From dc9a580617a838b63cbf5feae362b6f9cf5ed986 Mon Sep 17 00:00:00 2001 From: quazzie Date: Fri, 22 Mar 2013 10:35:50 +0100 Subject: [PATCH] fix($location): back-button should fire $locationChangeStart Before $locationChangeStart event is not broadcast when pressing the back-button on the browser. Closes #2109 --- src/ng/location.js | 4 ++++ test/ng/locationSpec.js | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ng/location.js b/src/ng/location.js index 9a5a68ba35d3..b6d14e1ac39c 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -546,6 +546,10 @@ function $LocationProvider(){ // update $location when $browser url changes $browser.onUrlChange(function(newUrl) { if ($location.absUrl() != newUrl) { + if ($rootScope.$broadcast('$locationChangeStart', newUrl, $location.absUrl()).defaultPrevented) { + $browser.url($location.absUrl()); + return; + } $rootScope.$evalAsync(function() { var oldUrl = $location.absUrl(); diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index bb0659a5d0bb..fb82804478ec 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -1237,7 +1237,7 @@ describe('$location', function() { expect($location.url()).toEqual(''); $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) { - throw Error('there is no before when user enters URL directly to browser'); + $log.info('start', newUrl, oldUrl); }); $rootScope.$on('$locationChangeSuccess', function(event, newUrl, oldUrl) { $log.info('after', newUrl, oldUrl); @@ -1247,6 +1247,8 @@ describe('$location', function() { $browser.url('http://server/#/somePath'); $browser.poll(); + expect($log.info.logs.shift()). + toEqual(['start', 'http://server/#/somePath', 'http://server/']); expect($log.info.logs.shift()). toEqual(['after', 'http://server/#/somePath', 'http://server/']); })