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

Browserurlchange #8788

Closed
wants to merge 2 commits 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
7 changes: 7 additions & 0 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ function Browser(window, document, $log, $sniffer) {
return callback;
};

/**
* Checks whether the url has changed outside of Angular.
* Needs to be exported to be able to check for changes that have been done in sync,
* as hashchange/popstate events fire in async.
*/
self.$$checkUrlChangedOutsideAngular = fireUrlChange;

//////////////////////////////////////////////////////////////
// Misc API
//////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ function $RootScopeProvider(){
logIdx, logMsg, asyncTask;

beginPhase('$digest');
// Check for changes to browser url that happened in sync with the call to $digest
$browser.$$checkUrlChangedOutsideAngular();

lastDirtyWatch = null;

Expand Down
2 changes: 2 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ angular.mock.$Browser = function() {
return listener;
};

self.$$checkUrlChangedOutsideAngular = angular.noop;

self.cookieHash = {};
self.lastCookieHash = {};
self.deferredFns = [];
Expand Down
54 changes: 54 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,60 @@ describe('$location', function() {
});


it('should update url when location changed outside of Angular by changing hash', function() {
var fakeWindow = {
addEventListener: angular.noop,
location: {
replace: angular.noop,
href: '/hello'
},
history: {
}
};

module(function($windowProvider, $locationProvider, $browserProvider) {
$locationProvider.html5Mode(true);
$browserProvider.$get = function($document, $log, $sniffer) {
var b = new Browser(fakeWindow, $document, $log, $sniffer);
b.pollFns = [];
return b;
};
});

inject(function($rootScope, $location) {
fakeWindow.location.href = '/hello#goodbye';
expect(function() {
$rootScope.$digest();
}).not.toThrow();
expect($location.hash()).toBe('goodbye');
expect($location.url()).toBe('/hello#goodbye');
});
});


it('should update url when location changed outside of Angular with replaceState', function() {
if (window.history.replaceState) {
module(function($windowProvider, $locationProvider, $browserProvider) {
$locationProvider.html5Mode(true);
$browserProvider.$get = function($document, $window, $log, $sniffer) {
var b = new Browser($window, $document, $log, $sniffer);
b.pollFns = [];
return b;
};
});

inject(function($rootScope, $browser, $location, $sniffer){
window.history.replaceState(null, '', '/hello');
// Verify that infinite digest reported in #6976 no longer occurs
expect(function() {
$rootScope.$digest();
}).not.toThrow();
expect($location.path()).toBe('/hello');
});
}
});


it('should rewrite when hashbang url given', function() {
initService(true, '!', true);
inject(
Expand Down