How to stop page leave


#1

I want to stop a page transition from within the onPageWillLeave hook. I want to perform some validation there, and then stop the user from leaving if the validation fails. Is this possible currently? Is there a better way that I should be aware of? Thanks!


#2

If you don’t rely on default navigation but do your navigation in your controller it’s easy.

For example, if you want to be able to control whether or not the back-button at the top of the screen goes back to the previous view, you could just define a new back-button in your template and handle the on-click for that button. An example:

The template:

<ion-view view-title="MyView">
    <ion-nav-buttons side="left">
        <button class="button button-clear icon ion-ios-arrow-back" ng-click="goBack()">Back</button>
    </ion-nav-buttons>
    <ion-content class="padding">
    ....
    </ion-content>
</ion-view>

In your controller:

 $scope.goBack = function()
 {
    if (inputIsValid()) {
       $ionicHistory.goBack();
    }
    else {
       alert("There's something wrong with your input");
    }
 };

#3

Thanks for your reply. I’m handling navigation in a separate component, a primary bar component, which is separate from the current component controller on screen, so this solution is not ideal for me. Also, I’m using Ionic 2.


#4

@lastmj Excuse me,Did you solve this problem?


#5

I just wanted to mention that there’s an open issue about this, in case that someone finds this topic: