diff --git a/src/ng/location.js b/src/ng/location.js index b4cbd5587a0c..bc075a352b85 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -904,6 +904,13 @@ function $LocationProvider() { // update $location when $browser url changes $browser.onUrlChange(function(newUrl, newState) { + + if (isUndefined(beginsWith(appBaseNoFile, newUrl))) { + // If we are navigating outside of the app then force a reload + $window.location.href = newUrl; + return; + } + $rootScope.$evalAsync(function() { var oldUrl = $location.absUrl(); var oldState = $location.$$state; diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 606aabff76b2..1efe3396762f 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -860,7 +860,6 @@ describe('$location', function() { }); }); - // location.href = '...' fires hashchange event synchronously, so it might happen inside $apply it('should not $apply when browser url changed inside $apply', function() { initService({html5Mode:false,hashPrefix: '!',supportHistory: true}); @@ -1150,6 +1149,19 @@ describe('$location', function() { expect($browserUrl.mostRecentCall.args).toEqual(['http://new.com/a/b/bar', false, null]); }); }); + + it('should force a page reload if navigating outside of the application base href', function() { + initService({html5Mode:true, supportHistory: true}); + mockUpBrowser({initialUrl:'http://new.com/a/b/', baseHref:'/a/b/'}); + + inject(function($window, $browser, $location) { + $window.location.href = 'http://new.com/a/outside.html'; + spyOn($window.location, '$$setHref'); + expect($window.location.$$setHref).not.toHaveBeenCalled(); + $browser.$$checkUrlChange(); + expect($window.location.$$setHref).toHaveBeenCalledWith('http://new.com/a/outside.html'); + }); + }); }); @@ -2540,8 +2552,10 @@ describe('$location', function() { win.addEventListener = angular.noop; win.removeEventListener = angular.noop; win.location = { - get href() { return parser.href; }, - set href(val) { parser.href = val; }, + get href() { return this.$$getHref(); }, + $$getHref: function() { return parser.href; }, + set href(val) { this.$$setHref(val); }, + $$setHref: function(val) { parser.href = val; }, get hash() { return parser.hash; }, // The parser correctly strips on a single preceding hash character if necessary // before joining the fragment onto the href by a new hash character