Is there a way to reload a state from a different state?


#1

I am using $stateProvider and I have a Friends tab that has a list of the authenticated users friends and a Settings tab that has a Logout button.

Is there a way to “clear” the friends that are loaded in the Friends tab when the user clicks Logout from the Settings tab? As of now, when you got back to the Friends tab after logging out, all of the friends are still listed.


#2

You could try to set cache: false in your Friend state definition so it will reload its controller every time your Friend state is invoked. It would look something like this:

$stateProvider.state("friends",
    {
        cache: false,
        url:...,
        templateUrl:...,
        controller: ...
    })

#3

cache: false is too excessive for my case. The Friends displays an $ionicLoading overlay as it fetches the friends data, so it’s a poor UX when swtiching tabs. I just need this functionality for the rare cases when someone logs out as user A and logs back in as user B.

I am looking for a way to do something like $state.init('tab.friends');


#4

Actually, is there a way to listen for an event that triggers whenever the FriendsCtrl is activated? That way I can just to the reload logic myself, when needed.


#5

There are events for exactly what you need: $ionicView.enter or $ionicView.leave.

How to use it in controller:

$scope.$on('$ionicView.enter', function(){
  // reload logic
});

More info about events can be found here:
http://ionicframework.com/docs/api/directive/ionView/


#6

Thanks! This is just what I was looking for!