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

Fixed inconsistency in $location.path() behaviour on the $locationChangeStart event when using back/forward buttons in the browser or manually changing the url in the address bar. #5089

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
13 changes: 7 additions & 6 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,16 +612,17 @@ 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();

$location.$$parse(newUrl);
afterLocationChange(oldUrl);
if ($rootScope.$broadcast('$locationChangeStart', newUrl,
oldUrl).defaultPrevented) {
$location.$$parse(oldUrl);
$browser.url(oldUrl);
} else {
afterLocationChange(oldUrl);
}
});
if (!$rootScope.$$phase) $rootScope.$digest();
}
Expand Down
25 changes: 25 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,31 @@ describe('$location', function() {
dealoc($rootElement);
});
});

it('should return the same $location.path() when $locationChangeStart event occurs no matter the cause of url change', function() {
inject(function($location, $rootScope, $browser, $window) {
var log = '',
base = $browser.url();

$rootScope.$on('$locationChangeStart', function() {
log += $location.path();
});

// change through $location service
$location.path('/b');
$rootScope.$apply();

// reset location
$location.path('');
$rootScope.$apply();

// change through $browser
$browser.url(base + '#/b');
$browser.poll();

expect(log).toEqual('/b//b');
});
});
});

describe('LocationHtml5Url', function() {
Expand Down