【发布时间】:2015-07-23 16:30:44
【问题描述】:
我正在尝试在我的 Angular 模块中实现一个函数,该函数将触发一个事件来保存页面上的更改。用户离开页面的不同方式是:
- 单击加载新模板的链接。
- 单击返回按钮
- 关闭选项卡/窗口
我的实现使用以下代码处理 #1:
.directive('confirmOnExit', function() {
return {
link: function($scope, elem, attrs) {
window.onbeforeunload = function(){
return "If you leave this page, you will exit the practice session. You will be able to view your score report for the completed session but will have to start a new one if you want to continue practicing.";
}
$scope.$on('$locationChangeStart', function(event, next, current) {
if($scope.$dirty & confirm("Are you sure you want to leave this practice session?")) {
console.log("Will end session!");
end_session();
}
});
}
};
});
但是,当 #2 或 #3 出现时,会显示弹出对话框,但似乎没有触发 end_session() 函数。
任何关于如何处理后退按钮和关闭选项卡/窗口的见解将不胜感激?
【问题讨论】:
标签: javascript angularjs django