我在我的服务中有一个
onbeforeUnload
事件的回调函数。在我的 app.run
块中,我写下了以下代码:
window.onbeforeunload = Services.beforeWindowClose;
this.beforeWindowClose = function (event) {
var currentState = $state.current.name;
if (currentState !== constant.LOGOUT) {
$rootScope.$broadcast("close");
if (Utilities.isSavePending)
event.returnValue = constant.MSG;
}
}
在我的控制器中我有:
$scope.$on("close", function () {
Utilities.isSavePending = vm.save; //sets it to true
})
现在,在Angular中,由于事件是同步的,这段代码应该在窗口关闭时弹出一个弹窗。然而,它直接关闭了我的窗口。
我的意图是,每当用户关闭窗口时,我会触发一个事件并查看我的控制器中是否有任何未保存的数据。如果有一些未保存的数据,则浏览器不应关闭,并给出一个弹窗,而如果没有未保存的数据,则浏览器应关闭。
我做错了什么或者理解有误吗?
return constant.MSG
,因为一些浏览器需要这样做。 - dloedaevent.returnValue = constant.MSG;
代码正在执行吗? - dloedaif(Utilities.isSavePending)
条件。浏览器确实会到这里来,但悬停在if(Utilities.isSavePending)
上却没有显示任何内容,然后if(Utilities.isSavePending)
就从未执行过,浏览器就直接关闭了。 - Saurabh Tiwari