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

Commit

Permalink
fix($location): back-button should fire $locationChangeStart
Browse files Browse the repository at this point in the history
Before $locationChangeStart event is not broadcast when pressing the back-button on the browser.

Closes #2109
  • Loading branch information
quazzie authored and petebacondarwin committed May 1, 2013
1 parent 660605b commit dc9a580
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 3 additions & 1 deletion test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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/']);
})
Expand Down

0 comments on commit dc9a580

Please sign in to comment.