Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ui-route exit page

Tags:

angularjs

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();
        }
    });
like image 237
quma Avatar asked Feb 19 '16 13:02

quma


1 Answers

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.

like image 189
beaver Avatar answered Oct 04 '22 04:10

beaver