javascript - How to detect browser back button click event using angular? -
is possible detect user entered page through using history button in browser? preferably want detect action using angular.js.
i not want use angular routing. should work if user submits form , after successful submit server , re-direct, should possible if user goes form using button of browser.
here solution angular.
myapp.run(function($rootscope, $route, $location){ //bind `$locationchangesuccess` event on rootscope, dont need //bind in induvidual controllers. $rootscope.$on('$locationchangesuccess', function() { $rootscope.actuallocation = $location.path(); }); $rootscope.$watch(function () {return $location.path()}, function (newlocation, oldlocation) { if($rootscope.actuallocation === newlocation) { alert('why did use history back?'); } }); });
i using run block kickstart this. first store actual location in $rootscope.actuallocation, listen $locationchangesuccess , when happens update actuallocation new value.
in $rootscope watch changes in location path , if new location equal previouslocation because $locationchangesuccess not fired, meaning user has used history back.
here fiddle.
Comments
Post a Comment