简体   繁体   中英

Can onbeforeunload event be watch by $watch in angular js?

I am a newbie of angularjs and now I am stack on catching event of onbeforeunload event. I would like to show to some response to user when they leave the page. And I would like to watch onbeforeunload event by $watch and execute my message display function.

But how can I watch that event? Could anybody please answer me? Thank you very much.

How about this:

$scope.$on('$locationChangeStart', function( event ) {
    var answer = confirm("Your message....");
    if (!answer) {
        event.preventDefault();
    }
});

With $watch which I think you don't need it in here. Something like this:

$scope.$watch(function(){
        return $location.path();
}, function(newPath, oldPath){
   // Compare it, and trigger your message function.
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM