$state.go problems


#1

Hey, i’m using $state.go() to switch the views, the problem i just noticed is, that this method in some cases like creates a new controller and each time i get an event the event is triggered +1 times, so this code:

$rootScope.$on('ChangePasswordAcc', function(event, data) {
    navigator.notification.alert('text', function() {$ionicLoading.hide();}, 'text');
    $state.go('tab.profile');
});

is giving me the Alert multiple times, in the first run i get only one popup on the event, on the second run i get 2, … and so on. Anyone knows this problem?

ADD: i know the $state.go is the problem, because if i remove it it’s working :wink:


#2

Triggered +1 times may be another problem. That is the $on method is something registered in the global scope, which means even if you leave a controller and destroy a scope, it is still there.
So if you have another $rootScope event registered, it will be two registered and ran two times.

The above situation was one of my past problem. A way to solve it is to give a var name to it.

var cleanListener=$rootScope.$on('ChangePasswordAcc', function(event, data) {
   $state.go('tab.profile');
});

And run it in the scope destory event.(All register method return a deregister function)

$scope.$on('$destroy',function(){
    cleanListener(); 
})

Hope this helps.