Is there a way to get the previous state of the current state?
For example I would like to know what the previous state was before current state B (where previous state would have been state A).
I am not able to find it in ui-router github doc pages.
var previous = $previousState. get(); //Gets a reference to the previous state. previous is an object that looks like: { state: fromState, params: fromParams } where fromState is the previous state and fromParams is the previous state parameters.
I use resolve to save the current state data before moving to the new state:
angular.module('MyModule')
.config(['$stateProvider', function ($stateProvider) {
$stateProvider
.state('mystate', {
templateUrl: 'mytemplate.html',
controller: ["PreviousState", function (PreviousState) {
if (PreviousState.Name == "mystate") {
// ...
}
}],
resolve: {
PreviousState: ["$state", function ($state) {
var currentStateData = {
Name: $state.current.name,
Params: angular.copy($state.params),
URL: $state.href($state.current.name, $state.params)
};
return currentStateData;
}]
}
});
}]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With