How to excecute the controller every time?


#1

Hi, I found the following: When I have a Tab App (ionic start myApp tabs) and then I click on every single tab.

After this, I set breakpoints to the Controllers ChatsCtrl and FriendsCtrl.

Not the problem: When I switch from Chat to friend and then to chat again, the breakpoint gets not called because the controller is not executed.

What I need to change to execute some code every time for a controller?


#2

This could be because your views are being cached. You can disable the view caching per view. Perhaps this will help http://ionicframework.com/docs/api/directive/ionNavView/


#3

I’m having this problem too. The want to be able to keep the page caching so the DOM doesnt have to load every time but I want the controller to refresh every time I go to the page so that all the data is updated.

Haven’t found a good way to do this as of yet. Hoping an only data refresh will be introduced or something!


#4

You can use $ionicView.enter to execute certain code each time the view is entered. There is also beforeEnter and afterEnter to execute the code before or after entering the view. An example of using it to track the view in Google Analytics:

$scope.$on('$ionicView.enter', function() {
    analytics.trackView('Screen Title');
});

You could create an initialization function (if you want to reset the data) and call it each time the view is entered.


#5

It works! tnx @brandyshea :slight_smile:


#6

I love you for this… :slight_smile: