Skip to content

Commit 753060b

Browse files
committed
feat($state): allow prevent syncUrl on failure
Check defaultPrevented before syncUrl on failure.
1 parent 5ee8d1e commit 753060b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/state.js

+5-2
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

+15
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)