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

Commit fc78156

Browse files
rkirovIgorMinar
authored andcommitted
fix($location): reset $location.$$replace with every watch call
Closes #1111
1 parent c9199ee commit fc78156

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/ng/location.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ function $LocationProvider(){
590590
var changeCounter = 0;
591591
$rootScope.$watch(function $locationWatch() {
592592
var oldUrl = $browser.url();
593+
var currentReplace = $location.$$replace;
593594

594595
if (!changeCounter || oldUrl != $location.absUrl()) {
595596
changeCounter++;
@@ -598,12 +599,12 @@ function $LocationProvider(){
598599
defaultPrevented) {
599600
$location.$$parse(oldUrl);
600601
} else {
601-
$browser.url($location.absUrl(), $location.$$replace);
602-
$location.$$replace = false;
602+
$browser.url($location.absUrl(), currentReplace);
603603
afterLocationChange(oldUrl);
604604
}
605605
});
606606
}
607+
$location.$$replace = false;
607608

608609
return changeCounter;
609610
});

test/ng/locationSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,29 @@ describe('$location', function() {
447447

448448
expect($browserUrl).toHaveBeenCalledOnce();
449449
expect($browserUrl.mostRecentCall.args).toEqual(['http://new.com/a/b#!/n/url', true]);
450+
expect($location.$$replace).toBe(false);
451+
}));
452+
453+
454+
it('should always reset replace flag after running watch', inject(function($rootScope, $location) {
455+
// init watches
456+
$location.url('/initUrl');
457+
$rootScope.$apply();
458+
459+
// changes url but resets it before digest
460+
$location.url('/newUrl').replace().url('/initUrl');
461+
$rootScope.$apply();
462+
expect($location.$$replace).toBe(false);
463+
464+
// set the url to the old value
465+
$location.url('/newUrl').replace();
466+
$rootScope.$apply();
467+
expect($location.$$replace).toBe(false);
468+
469+
// doesn't even change url only calls replace()
470+
$location.replace();
471+
$rootScope.$apply();
472+
expect($location.$$replace).toBe(false);
450473
}));
451474

452475

test/ng/routeSpec.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -560,20 +560,15 @@ describe('$route', function() {
560560
$routeProvider.when('/bar/:id', {templateUrl: 'bar.html'});
561561
$routeProvider.when('/foo/:id/:extra', {redirectTo: '/bar/:id'});
562562
});
563-
inject(function($route, $location, $rootScope) {
564-
var replace;
565-
566-
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
567-
if (oldUrl == 'http://server/#/foo/id3/eId') {
568-
replace = $location.$$replace;
569-
}
570-
});
563+
inject(function($browser, $route, $location, $rootScope) {
564+
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
571565

572566
$location.path('/foo/id3/eId');
573567
$rootScope.$digest();
574568

575569
expect($location.path()).toEqual('/bar/id3');
576-
expect(replace).toBe(true);
570+
expect($browserUrl.mostRecentCall.args)
571+
.toEqual(['http://server/#/bar/id3?extra=eId', true]);
577572
});
578573
});
579574
});

0 commit comments

Comments
 (0)