Angularjs 检测用户何时离开窗口

Angularjs 检测用户何时离开窗口,angularjs,Angularjs,我想检测用户何时使用AngularJS退出窗口。我的应用程序未使用ngRoute。因此,我不能使用$destroy或$locationChangeStart 你能帮我吗?你可以使用: 或使用: 卸载前的窗口的如何? $scope.$on('$locationChangeStart', function( event ) { var answer = confirm("Are you sure you want to leave this page?") if (!answer)

我想检测用户何时使用
AngularJS
退出窗口。我的应用程序未使用
ngRoute
。因此,我不能使用
$destroy
$locationChangeStart

你能帮我吗?

你可以使用:

或使用:


卸载前的
窗口
如何?
$scope.$on('$locationChangeStart', function( event ) {
    var answer = confirm("Are you sure you want to leave this page?")
    if (!answer) {
        event.preventDefault();
    }
});
window.onbeforeunload = function (event) {
  var message = 'Are you sure you want to leave this page?';
  if (typeof event == 'undefined') {
    event = window.event;
  }
  if (event) {
    event.returnValue = message;
  }
  return message;
}