2

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 app.state 1 to app.state 2. controller.js

 if(app.state1 to app.state2) {
    $scope.run();
    }

I have searched too but found nothing related to this.
Regards.

Atula
  • 2,177
  • 2
  • 12
  • 28

1 Answers1

1

Ionic routes used angular ui-router therefore event callbacks are used in the same way:

ui-router

Where appropriate:

$rootScope.$on('$stateChangeStart', function(ev, toState, toParams, fromState, fromParams){
    if(toState.name == 'stateA' && fromState.name == 'stateB') {
        // run your code
        $scope.run();
    }
});

(UPDATE)

if you need to ask for more than one state:

$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();
    }
});
  • one more question. Can we add multiple *fromState.name* ? – Atula May 02 '16 at 04:58
  • You're welcome, The fromState will always bring only the current state to which the router will go. If you want to ask for more than one state, you can use a single javascript code: `var states = ['stateB', 'stateC', 'stateD']; if(states.indexOf(fromState.name) > -1){ ... }` – felipecamposclarke May 02 '16 at 14:00
  • I am unable to do it right I think. Can you provide me full code as I can't understand what to write inside **{}**. – Atula May 03 '16 at 05:29
  • thank you so much.I think you are much aware of ionic so can you check this issue too http://stackoverflow.com/questions/37005207/ionic-hide-nav-bar-not-the-buttons – Atula May 04 '16 at 05:05
  • it is creating an error. I have set **hide-nav-bar="true"** on "stateB" then on coming back from stateB to stateA it shows my nav-bar. I have used a function to refresh a page inplace of *$scope.run() here. – Atula May 04 '16 at 05:53
  • @Atula I also answered your other question – felipecamposclarke May 04 '16 at 08:20