Confused over trivial app and controllers

Hi,

This is probably a really dumb question but I really can’t answer it myself so reaching out for some help.

I have created the dummy or sample Ionic tabs app. This is the one that creates a simple app that looks like you are chatting to somebody. On the bottom you have a Status, Chats Friends and Account button.

I can easily create it, build it, load it into Xcode and test it. That works fine.

I then added at the start of each controller in controllers.js a simple line

console.log(" entered");

I change the value of to be whatever the controller is. These additional console.log changes are the only changes I make.

I then rebuild it and deploy it to a simulator.

The deployment works fine and when I run it and clock on the buttons on the bottom, I get console output like this

2015-01-15 11:24:30.653 myApp[30912:79429] ChatsCtrl entered
2015-01-15 11:24:52.109 myApp[30912:79429] ChatDetailCtrl entered
2015-01-15 11:24:58.484 myApp[30912:79429] ChatDetailCtrl entered
2015-01-15 11:26:34.345 myApp[30912:79429] FriendsCtrl entered
2015-01-15 11:26:53.926 myApp[30912:79429] AccountCtrl entered

Which is exactly what I expect. As each controller is entered a debug line is put out.

However when I click on the Status button again, the correct screen pops up but no console message is written out. This is the same for Chats, Friends and Account, yet for ChatsDetailCtrl and FriendsDetailCtrl the console message saying the controller has been entered is written out and keeps getting written out each time.

I’m confused (not difficult) and wondered why this is happening.

The reason for this is I have used this as a sample and want something to happen each time I click on the button.

Any guidance appreciated.

Thanks,

Rob.

I assume you are using the latest Ionic build (Beta 14)? If so, Ionic has implemented view caching now, so if you haven’t got too many views or large lists in your app making big memory demands, then it is likely that the view will stay in memory in the background and will simply be brought to the foreground again when you call it, rather than being initialised again.

It is all about performance. Sometimes the caching is not a good thing though, so you can turn it off a couple of ways. My usual way of disabling it is in the app.js file, just add the following line into your .state definition:

 .state('app.song-detail', {
    cache: false,
    url: "/songs/song/:songId",
    views: {
      'menuContent': {
        templateUrl: "templates/song-detail.html",
        controller: 'SongDetailCtrl'
      }
    }
  })

Note: the cache: false, line in there - that will disable caching for this view/controller.

Ah!

Thank you.

I’ve been using Ionic for some months and all my old controllers work the way I thought they should. Its a new app I’m trying out and it didn’t work the way I thought it ought to

I have also just rebuilt my dev kit so I do have all the latest stuff.

My assumption was that I was being a muppet which is normally the case and missed something really, really easy.

I’m just installing Windows on my dev machine to go with OS X and will try it out when 179 Windows Updates have finished installing, probably a few hours time.

Thanks for taking the time to reply and with such a cogent answer.

Best wishes.

Rob

1 Like

Just to confirm that was the problem and it is now resolved.

Thanks,

Rob

Also, if you don’t want to disable caching on the view but want it to do something every time it’s entered, you can use $ionicView in the controller:

$scope.$on('$ionicView.beforeEnter', function() {
    $scope.form.$setUntouched();
});
2 Likes

Just to add to @brandyshea answer and the conversation and clear a few points up.

So since scopes are getting destroyed anymore, once a controller has fired, it will never fire again. This enables up to keep the date in that scope available and add features like swipe-to-go-back (which is in the works).

So instead of rebuilding scopes, we admit some events when ever the scopes get reconnected.