Ionic 2 ionViewCanLeave hook on pop


I am trying to intercept the back navigation for a page and do some validation before i allow it to go back or not,
also i had a button on the same page the push another page on it, now when i use the ionViewCanLeave hook it will be fired for both the nav pop and push, is there anyway that i can do my validation only on the page is poppedfor the stack?


I’m not sure if it’s the best way, but you can make a boolean variable, and every time you enter this page you’re going to set it to true and set it to false before calling this.nav.push(). And it will still remain true if you will call this.nav.pop(). Then you have just to check the value in ionViewCanLeave hook.


Thanks for the reply,
The navigation happens when the back button on nav bar is pressed or the hardware button, i don’t do the push or pop manually, that’s why i couldn’t use a flag to check the navigation type.


Can you show the code? It’s obvious that back button makes the pop and it’s automatic, but I guess there should be something that makes a push.


i have an Edit view which contains some fields that the user should edit, one of this fields works as an autocomplete, which when the user click on it, it will navigate to another page to search for an item, so i push the search page over the Edit page, when i do so the ionViewCanLeave is triggered, but i need to run a specific validation when the Edit page is popped from the stack not when i push another page over it
Edit page html:

autocomplete component Html:

autocomplete component ts:

now when i click the auto complete the edit page ionViewCanLeave will be triggered.


Just change the value before pushing another view and then check it in ionViewCanLeave hook


Thanks, i am working on a generic code in which this component doesn’t know what is the parent, but for now it seems this is the best I can do (passing the parent as an input to the component).
Thanks again :slight_smile:


The child component can also generate an event and parent element should listen to it.


Yes, thanks a lot for the help :slight_smile: