Prevent user to leave view if some condition

Hey Ionic community,

I was wondering how a can display an popup message if user leave a view before clicking on the save button for example.
I thinking that code below could be works but not really…

$scope.$on('$ionicView.beforeLeave', function(e) {

    if (condition) {   
       e.preventDefault(); // Prevent the user to leave the view


Thank you :wink:

you can use

$rootScope.$on('$stateChangeStart', (event, toState, toStateParams, fromState, fromStateParams) ->
  if !condition

In your main

Thank’s yurinondual,

I test this method directly in the controller and it works well !!

Thank’s again :wink: