2016-05-24 8 views
0

jQueryで動作するこのコードは、ユーザーがタブまたはウィンドウを閉じるときに多くの操作を実行できます。ユーザが閉じるタブまたはウィンドウでアクションを実行

コードはIE、Firefox、Chromeで動作しています。

AngularJsで行う方法はありますか?

<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> 

編集

私は

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"); 
    } 
} 

を試みたが、あなたがトリガされwindow.onunloadイベントを、必要なエラー

答えて

0

を取得します。 AngularJSでDocumentation on developer.mozilla.org

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

あなたは$ウィンドウサービスを使用することができます。

//inject $window into controller 
controller('test', ['$scope', '$window', function($scope, $window){ 
    $window.bind('unload', function(){ 
    //do stuff 
    } 
}]) 
関連する問題