Detect unsaved changes and alert user using angularjs

前端 未结 7 1684
执念已碎
执念已碎 2020-11-29 18:50

Below is the code so far

    


    

        
7条回答
  •  野性不改
    2020-11-29 19:37

    To use Anders Ekdahl's excellent answer with an Angular 1.5 component, inject the $scope in the component's controller:

    angular
      .module('myModule')
      .component('myComponent', {
        controller: ['$routeParams', '$scope',
          function MyController($routeParams, $scope) {
            var self = this;
    
            $scope.$on('$locationChangeStart', function (event, next, current) {
              if (self.productEdit.$dirty && !confirm('There are unsaved changes. Would you like to close the form?')) {
                event.preventDefault();
              }
            });
          }
        ]
      });
    

提交回复
热议问题