2
votes

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 e.g 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?

1

1 Answers

0
votes

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?"; 
};