user1242321
user1242321

Reputation: 1696

Ionic 1.3 hides NavBar when caching is disabled

I started working with ionic recently and ran into an issue where my data was not being updated on re-entering the views. On finding more about ionic, I realized we can disable the caching for that view and Ionic is forced to recreate the view each time.

However, when I use $state.go('stateName',{},{reload: true}) and ionic caching disabled, I get another issue.

My controller is called twice for that view and the navbar disappears.

The same issue is open at ionic here. Also there has been some discussion about the issue here on ionic forum. However, the proposed solution of marking hide-nav-bar="false" does not work for me.

With the solution of

$scope.$on('$ionicView.enter', function(e) {
      $ionicNavBarDelegate.showBar(true);
    });

My navbar becomes visible but with NO buttons, and also, the controller is being called twice.

Since this should be a very very common scenario for any app which does not need view caching, could anyone please share any appropriate workaround figured out to get past this?

Thanks for the help!

Upvotes: 0

Views: 407

Answers (1)

Aditya Singh
Aditya Singh

Reputation: 16660

Don't use {reload: true} in your $state.go method to update the data in your view because this is what is causing the controller to be called twice.

Since ionic caches the controller, in order to update data every time you redirect to the view you can use the $ionicView lifecycle hooks namely beforeEnter, enter, loaded etc.

.controller('AppController', function ($scope) {
   $scope.$on('$ionicView.beforeEnter', function () {
     // Fetch your data here so that it will be updated every time you redirect to this view
   })
});

This is a very scalable approach for fetching data in controllers

More details about the lifecycle methods can be found here: http://ionicframework.com/docs/v1/api/directive/ionView/

Upvotes: 1

Related Questions