Anoop M M
Anoop M M

Reputation: 317

$state.go() doesn't load controller in ionic application

Using the following code, when a page with id=0 loads first time there is no problem with controller. But when again the same page loads with same id=0 again, it does not loads controller.

$state.go('tab.dash', {
  id: $rootScope.products[CONSTANTS.i].id
}, {
  reload: true
});

How does it happen? Please suggest me a solution.

Upvotes: 5

Views: 1308

Answers (2)

johnny
johnny

Reputation: 2122

well, When you cache the view (by default it is true) controller is loaded only at first time and on subsequent navigation it will attach and detach the scope. Cacheing helping with the performance of single page applications. If you dont want to disable the caching then you use the ionic view events like (enter, leave,loaded and so on).

 $scope.$on('$ionicView.enter', function () {
            // ur stuff in here....
        });

Upvotes: 0

Eric Himmelreich
Eric Himmelreich

Reputation: 437

I encountered a similar problem where I needed stats to recalculate every time a tab was visited.

You need to disable view caching. You can do so in the route setup. For example:

.state('tab.stats', {
 url: '/stats',
 views: {
   'tab-stats': {
     templateUrl: 'templates/tab-stats.html',
     controller: 'StatsCtrl'
   }
 },
 cache: false
})

Upvotes: 3

Related Questions