2016-05-24 9 views
0

Ich habe diesen Code, der mit jQuery funktioniert, kann ich viele Operationen ausführen, wenn Benutzer Tab oder Fenster zu schließen.Aktion auf Benutzer schließen Registerkarte oder Fenster

Code funktioniert in IE, Firefox und Chrome.

Gibt es eine Möglichkeit, dies in AngularJs zu tun?

<script type="text/javascript"> 
    var validNavigation = false; 

    function wireUpEvents() { 
     var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation 
     var leave_message = ' '; 
     function quitWdindow(e) { 
      if (!validNavigation) { 
       return leave_message; 

      } 
     } 
     window.onbeforeunload=quitWdindow; 

     // Attach the event keypress to exclude the F5 refresh 
     $(document).bind('keypress', function(e) { 
     if (e.keyCode == 116){ 
      validNavigation = true; 
     } 
     }); 

     // Attach the event click for all links in the page 
     $("a").bind("click", function() { 
     validNavigation = true; 
      }); 
      // Attach the event submit for all forms in the page 
      $("form").bind("submit", function() { 
      validNavigation = true; 
     }); 

     // Attach the event click for all inputs in the page 
     $("input[type=submit]").bind("click", function() { 
     validNavigation = true; 
     }); 

    } 

    // Wire up the events as soon as the DOM tree is ready 
    $(document).ready(function() { 
     wireUpEvents(); 
    }); 
</script> 

bearbeiten

versuchte ich

angular.module('My.App').controller('MainController', MainController); 

function MainController($log, $state, $AppAuthenticationService, $CoreWindowService, $AppConfiguration, $filter, $window){ 
    var vm = this; 

    ... 
    ... 
    $window.bind('unload', function(){ 
     alert("unload"); 
    } 
} 

aber

Antwort

0

Sie benötigen window.onunload Ereignisfehler erhalten, das ausgelöst wird. Documentation on developer.mozilla.org

window.onunload = function(){ 
    alert('Are you sure?'); 
} 

In AngularJS kann $ Fenster Service nutzen:

//inject $window into controller 
controller('test', ['$scope', '$window', function($scope, $window){ 
    $window.bind('unload', function(){ 
    //do stuff 
    } 
}]) 
Verwandte Themen