This feature was suggested in the ui-router repository (Github Issue (in English)) and seems to have a feedback positive, but for now there is still no simplified way to get this information.
A possible workaround pointed out in the comments of Issue previously mentioned is to listen to the event $stateChangeSuccess
in $rootScope
, whose callback receives the object of $state
as an argument, thus being able to save it in the $rootScope
. Thus, the data from the $state
are accessible in all scopes inherited from the $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);
});