Skip to content

Commit 75b5af8

Browse files
Michael TempestMichael Tempest
Michael Tempest
authored and
Michael Tempest
committed
Added a .back method to ui-router, fixes angular-ui#92
Adds $state.previous and $state.previousParams. Implements $state.back method which wraps $state.transitionTo and can take an options object which is passed through. $state.previous and $state.previousParams are only updated when the transition completes. Added unit tests for .back method()
1 parent f17b587 commit 75b5af8

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/state.js

+30
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
469469
params: {},
470470
current: root.self,
471471
$current: root,
472+
previous: undefined,
473+
previousParams: undefined,
472474
transition: null
473475
};
474476

@@ -533,6 +535,32 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
533535
return this.transitionTo(to, params, extend({ inherit: true, relative: $state.$current }, options));
534536
};
535537

538+
/**
539+
* @ngdoc function
540+
* @name ui.router.state.$state#back
541+
* @methodOf ui.router.state.$state
542+
*
543+
* @description
544+
* Convenience method for transitioning back to previous state. `$state.back` calls
545+
* `$state.transitionTo` internally.
546+
*
547+
* @example
548+
* <pre>
549+
* var app = angular.module('app', ['ui.router.state']);
550+
*
551+
* app.controller('ctrl', function ($scope, $state) {
552+
* $scope.changeState = function () {
553+
* $state.back();
554+
* };
555+
* });
556+
* </pre>
557+
*
558+
* @param {object} options If Object is passed, object is an options hash.
559+
*/
560+
$state.back = function (options) {
561+
return this.transitionTo($state.previous, $state.previousParams, options);
562+
};
563+
536564
/**
537565
* @ngdoc function
538566
* @name ui.router.state.$state#transitionTo
@@ -686,6 +714,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
686714
if ($state.transition !== transition) return TransitionSuperseded;
687715

688716
// Update globals in $state
717+
$state.previous = $state.current;
718+
$state.previousParams = $state.params;
689719
$state.$current = to;
690720
$state.current = to.self;
691721
$state.params = toParams;

test/stateSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,26 @@ describe('state', function () {
483483
}));
484484
});
485485

486+
describe('.back()', function () {
487+
it('transitions to previous state', inject(function ($state, $q) {
488+
$state.transitionTo('about.person.item'); $q.flush();
489+
$state.go('^.^.sidebar'); $q.flush();
490+
$state.back(); $q.flush();
491+
492+
expect($state.$current.name).toBe('about.person.item');
493+
494+
}));
495+
496+
it('transitions to previous state', inject(function ($state, $stateParams, $q) {
497+
$state.transitionTo('about.person.item', { id:5 }); $q.flush();
498+
$state.go('^.^.sidebar'); $q.flush();
499+
$state.back(); $q.flush();
500+
501+
expect($stateParams).toEqual({ person: '', id: '5' });
502+
}));
503+
504+
});
505+
486506
describe('.reload()', function () {
487507
it('should reload the current state with the current parameters', inject(function ($state, $q, $timeout) {
488508
$state.transitionTo('resolveTimeout', { foo: "bar" });

0 commit comments

Comments
 (0)