我想用AngularJS
检测用户退出窗口的时间。我的应用不使用ngRoute
。因此,我无法使用$destroy
或$locationChangeStart
。
你能帮助我吗?
答案 0 :(得分:3)
您可以使用$locationChangeStart:
$scope.$on('$locationChangeStart', function( event ) {
var answer = confirm("Are you sure you want to leave this page?")
if (!answer) {
event.preventDefault();
}
});
或使用onbeforeunload:
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;
}