简体   繁体   中英

Handle browser back button with angular in controller

I want to handle browser back button with angular, i have tried many solution but i cant find one which is fulfill my requirement, here is my tried code .

$scope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
   if(!allowed /* inject your logic here */) {
       event.preventDefault();
    }

});

it is working fine when i go to particular page from my app, but if go from eg www.google.com direct to that particular page by pasting link then it just get back to Google.

what is actually i want handle both scenario of back button.
is it possible?

I will really appreciate your response,
these are the links I have tried but problem remain same.
How to detect browser back button click event using angular?
How to handle browser back button event in a particular controller?

Have you tried using 'window.onbeforeunload'?

For example:

window.onbeforeunload = function() 
{ 
   //Do whatever here for example prompt user 
   return "Are you sure you want to go back?"; 
};

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