This feature was suggested in the ui-router repository ( GitHub issue ) and seems to have a positive feedback , but for now there is no simplified way to get this information.
A possible workaround mentioned in the issue comments mentioned above is to listen for the $stateChangeSuccess
event in $rootScope
, whose callback receives the object of the previous $state
as argument, so you can save it in $rootScope
. This way, data from $state
previous is accessible in all scopes that inherit from $rootScope
. Plunker :
app.run(function ($rootScope) {
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
$rootScope.previousState = fromState;
});
})
.controller("State1Ctrl", function($scope, $state) {
console.log($scope.previousState, $scope.previousState.name);
})
.controller("State2Ctrl", function($scope, $state) {
console.log($scope.previousState, $scope.previousState.name);
});