Reset DOM/View on Switching State/Location


#1

Hello IonicFrameworkForums!

I am currently working on an application where I am submitting a form (on page /feature/xxx), and on submitting that form, it takes me to the page where all entries to that form can be seen.

At the end of the createEntry function, I am changing the path with

$location.path("/feature/yyy");

and directly after that in the same function I want to clear the cache/history/whatever and reset the DOM for that view completely.

I thought that using

$ionicHistory.clearCache();

would work, but it doesn’t seem to do anything.

Is there any way to just reset the DOM for this view before exiting? I’ve tried ordering it the opposite way, thus-so…

$ionicHistory.clearCache();
$location.path("/feature/xxx");

but that doesn’t work either.

Or is there a way that when I navigate to the new state/location that I could ensure it is cleared when I visit it?

Thank you!


#2

Hello all…

I’ve updated my template file to have

<ion view cache-view="false">
....
</ion-view>

and have removed the $ionicHistory and any other things, and it seems to have done what I needed to do. Not sure if it’s gonna mess with anything else in the future :stuck_out_tongue:


#3

Good you got it working, you could do this in the $ionicView.beforeLeave() event and that will fire when I think you want it to.


#4

@bradmartin,

to do $ionicView i’d have to include this as a paramater for the controller, yes?

And then just call $ionicView.beforeLeave(); and it’ll do what I want?

Still trying to wrap my head around all of this. Either way, it’s a pretty awesome tool, Ionic.


#5

No need to pass $ionicView. Just use the following, it doesn’t automatically do what you want but this might be the best time to do what you want with removing the DOM. What you are doing is probably fine though, if it’s working :slight_smile:

I just try to use the ionicView life cycle events because it can really help smooth out things on Android devices related to the transitions and I think it’s better organized to use the events instead of hooking it manually at a callback or something else.

    $scope.$on('$ionicView.beforeLeave', function () {
           // Do your work here
    });