Skip to content

Commit 3a1308b

Browse files
fix(Transition): Reset URL to current state after aborted transition
If the user changd the url, we started a transition to a state, and then cancelled, the url remained set to the cancelled transition's tostate's url. closes #2611
1 parent 4b6d56f commit 3a1308b

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/state/hooks/transitionManager.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {TargetState} from "../targetState";
1414
import {ViewHooks} from "./viewHooks";
1515
import {EnterExitHooks} from "./enterExitHooks";
1616
import {ResolveHooks} from "./resolveHooks";
17+
import {UrlRouter} from "../../url/urlRouter";
1718

1819
/**
1920
* This class:
@@ -41,7 +42,7 @@ export class TransitionManager {
4142
constructor(
4243
private transition: Transition,
4344
private $transitions,
44-
private $urlRouter,
45+
private $urlRouter: UrlRouter,
4546
private $view, // service
4647
private $state: StateService,
4748
private $stateParams, // service/obj
@@ -108,6 +109,10 @@ export class TransitionManager {
108109
if (error.type === RejectType.SUPERSEDED && error.redirected && error.detail instanceof TargetState) {
109110
return this._redirectMgr(transition.redirect(error.detail)).runTransition();
110111
}
112+
113+
if (error.type === RejectType.ABORTED) {
114+
this.$urlRouter.update();
115+
}
111116
}
112117

113118
this.$transitions.defaultErrorHandler()(error);

src/url/urlRouter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export class UrlRouter {
342342
return this.listener = this.listener || $location.onChange(evt => update(this.urlRouterProvider.rules, this.urlRouterProvider.otherwiseFn, evt));
343343
}
344344

345-
update(read) {
345+
update(read?) {
346346
if (read) {
347347
this.location = $location.url();
348348
return;

test/stateSpec.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ describe('otherwise and state redirects', function() {
20952095
});
20962096

20972097

2098-
describe('hook redirects', function() {
2098+
describe('transition hook', function() {
20992099
var log, resolvelog;
21002100
beforeEach(module(function ($stateProvider, $urlRouterProvider) {
21012101
log = resolvelog = "";
@@ -2118,7 +2118,7 @@ describe('hook redirects', function() {
21182118
}));
21192119

21202120
// Test for #2455
2121-
it("from .otherwise() should go to the redirect-to target state and url", inject(function($transitions, $q, $state, $location) {
2121+
it("redirects from .otherwise() should go to the redirect-to target state and url", inject(function($transitions, $q, $state, $location) {
21222122
$transitions.onBefore({ to: 'home' }, function() {
21232123
return $state.target('loginPage', {}, { location: true });
21242124
});
@@ -2128,7 +2128,7 @@ describe('hook redirects', function() {
21282128
}));
21292129

21302130
// Test for #2537
2131-
it("should be able to change option.reload", inject(function($transitions, $q, $state, $trace) {
2131+
it("redirects should be able to change option.reload", inject(function($transitions, $q, $state, $trace) {
21322132
var count = 0;
21332133
$q.flush();
21342134
expect($state.current.name).toBe("home");
@@ -2151,7 +2151,7 @@ describe('hook redirects', function() {
21512151
}));
21522152

21532153
// Test for #2539
2154-
it("should re-resolve when reloading during a redirect", inject(function($transitions, $q, $state, $trace) {
2154+
it("redirects should re-resolve when reloading during a redirect", inject(function($transitions, $q, $state, $trace) {
21552155
var count = 0;
21562156
$q.flush();
21572157

@@ -2173,4 +2173,14 @@ describe('hook redirects', function() {
21732173
expect($state.current.name).toBe("home");
21742174
expect(resolvelog).toBe("fooResolve;fooResolve;");
21752175
}));
2176+
2177+
// Test for #2611
2178+
it("aborts should reset the URL to the prevous state's", inject(function($transitions, $q, $state, $location) {
2179+
$q.flush();
2180+
$transitions.onStart({ to: 'home.foo' }, function() { return false; });
2181+
$location.path('/home/foo'); $q.flush();
2182+
expect($state.current.name).toBe("home");
2183+
expect($location.path()).toBe('/home');
2184+
}));
2185+
21762186
});

0 commit comments

Comments
 (0)