Detect Specific .state change


#1

Is there any method to detect Particular states change only like in my case
I want to run a function only if we go from .state 1 to .state 2.

if(.state1 to .state2) {
$scope.run();
}
I have searched too but found nothing related to this.
Regards.


#2

You could track history and read it on each route transition

$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
    console.log('Next route: ', toState.name); 
    console.log("Previous route: ", $ionicHistory.currentStateName());
});

The first one tell you which route are you trying to transition and the second the last route.


#3

Thank you @gil13 for responding,
but I have got what I exactly need.

$rootScope.$on(’$stateChangeStart’, function(ev, toState, toParams, fromState, fromParams){
var states = [‘stateB’, ‘stateC’, ‘stateD’];
if(toState.name == ‘stateA’ && states.indexOf(fromState.name) > -1) {
// run your code
$scope.run();
}
});