UI-Router - scope not destroyed on state change? UI-Router - scope not destroyed on state change? angularjs angularjs

UI-Router - scope not destroyed on state change?


If we want to do something with

1) $rootScope inside of the controller (which has very limited lifetime),

2) we must destroy that, when controller (its $scope in fact) is being destroyed

So, this is the way how to hook and unhook

// get remove functionvar removeMe = $rootScope.$on("eventCT2", ...);// call that function$scope.$on("$destroy", removeMe)

But, in the case above, we should not even try to

1) create some controller action for one state...

2) and expect it will be called in another controller from different state

These will never live together


If you are using Ionic with Angular, you could use the life cycle events like so:

$scope.$on("$ionicView.beforeEnter", function(){   //Do something every time this controller is the active scope.})

You could play around with the other events provided in the above link as well. And it's probably best practise to minimize the use of $emit, which will lead to more predictable code and fewer state mutations.