Skip to content

Commit 640586b

Browse files
author
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.
1 parent f17b587 commit 640586b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-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;

0 commit comments

Comments
 (0)