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

fix($location): back-button not triggering $locationChangeStart #2206

Closed
wants to merge 1 commit into from
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
4 changes: 4 additions & 0 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,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 @@ -1216,7 +1216,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 @@ -1226,6 +1226,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