Skip to content

Commit e00aa69

Browse files
fix($state): statechangeCancel: don't clobber url if a new transition has started
Closes #2238 Closes #2229 Closes #2185 Closes #2236 Closes #2098 Closes #600
1 parent 350d3e8 commit e00aa69

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/state.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
10841084
*/
10851085
if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams, options).defaultPrevented) {
10861086
$rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
1087-
$urlRouter.update();
1087+
//Don't update and resync url if there's been a new transition started. see issue #2238, #600
1088+
if ($state.transition == null) $urlRouter.update();
10881089
return TransitionPrevented;
10891090
}
10901091
}

test/stateSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -1648,3 +1648,32 @@ describe('$stateParams', function () {
16481648
expect($stateParams.foo).toBeUndefined();
16491649
}));
16501650
});
1651+
1652+
// Test for #600, #2238, #2229
1653+
describe('otherwise and state redirects', function() {
1654+
beforeEach(module(function ($stateProvider, $urlRouterProvider) {
1655+
$urlRouterProvider.otherwise('/home');
1656+
$stateProvider
1657+
.state('home', { url: '/home', template: 'home' })
1658+
.state('loginPage', { url: '/login', templateUrl: 'login.html' });
1659+
}));
1660+
1661+
beforeEach(inject(function ($rootScope, $state) {
1662+
1663+
$rootScope.$on('$stateChangeStart', function (event, toState) {
1664+
if (toState.name !== "loginPage") {
1665+
event.preventDefault();
1666+
$state.go('loginPage', { redirectUrl: toState.name });
1667+
}
1668+
});
1669+
}));
1670+
1671+
iit("should not go into an infinite loop", inject(function($location, $rootScope, $state, $urlRouter, $httpBackend) {
1672+
$httpBackend.expectGET("login.html").respond("login page");
1673+
$location.url("notmatched");
1674+
$urlRouter.update(true);
1675+
expect(function() { $httpBackend.flush(); }).not.toThrow();
1676+
expect(function() { $rootScope.$digest(); }).not.toThrow();
1677+
expect($state.current.name).toBe("loginPage")
1678+
}));
1679+
});

0 commit comments

Comments
 (0)