How to know which State I'm coming from, using ui-router?

1

I'm using Ui-router's Angular-Ui and the only thing I need to know is which state I'm coming from. I already have for example;

app.controller("MainCtrl", function($scope,$state) {
    //$state.current ok, mas de onde eu vim?    
});

How to know which previous state called current ?

    
asked by anonymous 27.05.2014 / 23:05

1 answer

2

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);
});
    
28.05.2014 / 00:02