Prompt confirm before Leaving edited html form

后端 未结 6 1357
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 18:50

If user tries to leave unsaved edited form, a message box pop-up

\"This page is asking you to confirm that you want to leave - data you have entered may not be saved. Le

6条回答
  •  不要未来只要你来
    2021-02-04 19:14

    Warlock's answer is partly right, but in doing so, you'd break testability.

    In Angular, you'd want to use $window, and you'll want to watch $dirty on your form. You'll need to have a named form, notice name="myForm" below. Then you can $watch $dirty on the form in your $scope:

    app.controller('MainCtrl', function($scope, $window) {
      $scope.name = 'World';
      var win = $window;
      $scope.$watch('myForm.$dirty', function(value) {
        if(value) {
          win.onbeforeunload = function(){
            return 'Your message here';
          };
        }
      });
    });
    

    HTML

    Name:

    Here's a plunk to demonstrate: http://plnkr.co/edit/3NHpU1

提交回复
热议问题