AngularJS / Ionic refresh when view loads

By default a controller only fires it’s init code when a view is first loaded. If you are using Tabs and move away, then come back, the controller will not fire again. To get it to fire again when the page reloads, refer to these:

http://stackoverflow.com/questions/27058276/run-a-controller-function-whenever-a-view-is-opened-shown
shows:

$rootScope.$on('$stateChangeSuccess', 
function(event, toState, toParams, fromState, fromParams){ ... })

and maybe this method:

.controller('MyCtrl', function($scope) {
  $scope.$on('$ionicView.enter', function() {
     // Code you want executed every time view is opened
     console.log('Opened!')
  })
})

There is also mention of how to turn this off for the entire app, but I won’t copy that here as I don’t want to do that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s