Discard changes dialog

Hello I want to open dialog “Discard changes” in my app.
This dialog should open when you trying to go back when your form has content.
Example from Facebook app:

The form in my app is in modal, and it’s content is $scope.comment.data.content.

Code example (without back button event):

// On click back button
if($scope.comment.data.content.length > 0) {
    $cordovaDialogs.confirm('If you go back now, your draft will be discarded.', 'Discard changes', [
        'Discard',
        'Keep'
    ]).then(function(buttonIndex) {
        if(buttonIndex == 1) {
            // Go back
        } else if(buttonIndex == 2) {
            // Stay here
        }
    });
} else {
    // Go back
}
var handler = $scope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { 
});

You should check the to and from state params in addition, and check if the page has some content.

Also dont forget to “unsub” from $stateChangeStart by calling handler variable on ‘$destroy’ event.

Actually I find something that I think is good for me:
http://ionicframework.com/docs/api/service/$ionicPlatform/#registerBackButtonAction

I didn’t tried it yet, but I think it would do the job.

Thanks anyway my friend.