Skip to content

Commit a74ac92

Browse files
committed
feat($state): allowing transition to replace URL
Closes #409
1 parent 973944f commit a74ac92

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/state.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
388388
var toNav = to.navigable;
389389
if (options.location && toNav) {
390390
$location.url(toNav.url.format(toNav.locals.globals.$stateParams));
391+
392+
if (options.location === 'replace') {
393+
$location.replace();
394+
}
391395
}
392396

393397
$rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams);

test/stateSpec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,38 @@ describe('state', function () {
608608
$rootScope.$apply();
609609
expect($state.current.name).toBe('');
610610
}));
611+
612+
it('should replace browser history when "replace" enabled', inject(function ($state, $rootScope, $location, $q) {
613+
var originalReplaceFn = $location.replace, replaceWasCalled = false;
614+
615+
// @todo Replace this with a spy
616+
var decoratedReplaceFn = function() {
617+
replaceWasCalled = true;
618+
originalReplaceFn.call($location);
619+
};
620+
$location.replace = decoratedReplaceFn;
621+
622+
$state.transitionTo('about', {}, { location: 'replace' });
623+
$q.flush();
624+
625+
expect(replaceWasCalled).toEqual(true);
626+
}));
627+
628+
it('should not replace history normally', inject(function ($state, $rootScope, $location, $q) {
629+
var originalReplaceFn = $location.replace, replaceWasCalled = false;
630+
631+
// @todo Replace with spy
632+
var decoratedReplaceFn = function() {
633+
replaceWasCalled = true;
634+
originalReplaceFn.call($location);
635+
};
636+
$location.replace = decoratedReplaceFn;
637+
638+
$state.transitionTo('about');
639+
$q.flush();
640+
641+
expect(replaceWasCalled).toEqual(false);
642+
}));
611643
});
612644

613645
describe('default properties', function() {

0 commit comments

Comments
 (0)