Skip to content

Commit a89be9a

Browse files
committed
Merge pull request #967 from esion/sync_url_default_prevented
check defaultPrevented before sync url on failure
2 parents 5ee8d1e + 753060b commit a89be9a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/state.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
946946
* @param {Object} fromParams The params supplied to the `fromState`.
947947
* @param {Error} error The resolve error object.
948948
*/
949-
$rootScope.$broadcast('$stateChangeError', to.self, toParams, from.self, fromParams, error);
950-
syncUrl();
949+
evt = $rootScope.$broadcast('$stateChangeError', to.self, toParams, from.self, fromParams, error);
950+
951+
if (!evt.defaultPrevented) {
952+
syncUrl();
953+
}
951954

952955
return $q.reject(error);
953956
});

test/stateSpec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,21 @@ describe('state', function () {
777777
expect($state.current.name).toBe("about");
778778
}));
779779

780+
it('should not revert to last known working url on state change failure', inject(function ($state, $rootScope, $location, $q) {
781+
$state.transitionTo("about");
782+
$q.flush();
783+
784+
$rootScope.$on("$stateChangeError", function(event){
785+
event.defaultPrevented = true;
786+
});
787+
788+
$location.path("/resolve-fail");
789+
$rootScope.$broadcast("$locationChangeSuccess");
790+
$rootScope.$apply();
791+
792+
expect($location.path()).toBe("/resolve-fail");
793+
}));
794+
780795
it('should replace browser history when "replace" enabled', inject(function ($state, $rootScope, $location, $q) {
781796
var originalReplaceFn = $location.replace, replaceWasCalled = false;
782797

0 commit comments

Comments
 (0)