I need a possibility to ask the user if he/she is leaving a page. I read that his would be a possibility but the event is fired when I enter the page and not when I leave the page. onExit would work but this event I have to defined at the ... .routes.js and I need access to properties and functions at the controller. Is there an event that is fired by exit of page?
$scope.$on('$locationChangeStart', function( event ) {
var answer = confirm("Are you sure you want to leave this page?")
if (!answer) {
event.preventDefault();
}
});
The $stateChangeStart you mentioned is good for your needs: in fact from ui-router documentation:
$stateChangeStart - fired when the transition begins.
that is when user is leaving the previous state.
Here you can find an answer to a very similar question:
Can I stop the transition to the next state in an onExit?
and ther is also a working snippet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With