window.onbeforeunload for browser back button for intra page navigation in angularjs app

2,635 views
Skip to first unread message

jay raj

unread,
Feb 15, 2013, 5:47:38 AM2/15/13
to ang...@googlegroups.com
Here is my directive code

angular.module('myApp.directives', []).directive('confirmOnExit', function() {
    return {
        link: function($scope, elem, attrs) {


            $scope.$on('$locationChangeStart', function(event, next, current) {
                alert('me');
                if ($scope.myForm.$dirty) {
                    if(!confirm("Ahh the form is dirty, do u want to continue?")) {
                        event.preventDefault();
                    }
                }
            });

            window.onbeforeunload = function(){
                alert('me');
                if ($scope.myForm.$dirty) {
                    return "The Form is Dirty.";
                }
            }
        }
    };
});
Its not working with browser back button when having unsaved data during intra page navigation.But works with browser close and reload. Any pointers where i may be going wrong.Or is dere any other way to get this working.
Here is the link to stackoverflow also : : http://stackoverflow.com/questions/14892103/window-onbeforeunload-not-working-for-intra-page-navigation-in-angularjs-app

Clint Checketts

unread,
Feb 15, 2013, 8:19:51 AM2/15/13
to ang...@googlegroups.com
Do you need to fire this on $routeChangeStart too? That way it will warn when switching views in the same Angular app.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

jake

unread,
Feb 15, 2013, 8:52:20 AM2/15/13
to ang...@googlegroups.com
Well i'm not able to fire this on $routeChangeStart though.Dont know why.Can u make it fire on $routeChangeStart or any other way?
Reply all
Reply to author
Forward
0 new messages