Javascript 如何使用Angular 5处理页面关闭时的警报(离开或取消)?
当用户触发页面关闭时,将显示JS警报弹出窗口Javascript 如何使用Angular 5处理页面关闭时的警报(离开或取消)?,javascript,angular,Javascript,Angular,当用户触发页面关闭时,将显示JS警报弹出窗口 现在,我如何处理用户是否单击了“离开”按钮或“取消”按钮,在离开时将触发“卸载”事件。我不确定是否能正确地捕捉到取消 const confirmed = window.confirm('are you sure?'); if (confirmed) { // user confimed } else { // user did not confirm } 如果要阻止用户离开页面,必须将canDeactivate guard添加到该组件
现在,我如何处理用户是否单击了“离开”按钮或“取消”按钮,在离开时将触发“卸载”事件。我不确定是否能正确地捕捉到取消
const confirmed = window.confirm('are you sure?');
if (confirmed) {
// user confimed
} else {
// user did not confirm
}
如果要阻止用户离开页面,必须将canDeactivate guard添加到该组件路由定义中
更多信息这是我的代码main.html
<html>
<div class="row">
<div class="col-sm-3 columns">
<button type="" class="btn btn-info" ng-click=“submit()”> Submit </button>
</div>
</html>
现在将此代码添加到控制器中
在这段代码中,我只是通过使用模态模板的id调用模板。调用两个函数cancel和leave,通过这两个函数,模态将根据函数关闭
$scope.submit= function ( ) {
alertModalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: 'AlertpopupModal.html',
scope: $scope
});
$scope.leave = function () {
console.log("leave");
alertModalInstance.close(true);
};
$scope.cancel = function () {
console.log("cancel");
alertModalInstance.dismiss('cancel');
};
试试这个:
export class AppComponent {
title = 'leave-site';
@HostListener('window:beforeunload', [ '$event' ]) beforeunloadHandler(event) {
return false;
}
}
您可以禁用所有表单元素-这是一个猜测,因为我自己从来没有这样做过。我倾向于让所有浏览器操作保持原样。谢谢您的回复。但这段代码与我上面发布的问题无关。您的代码可能会解决自定义模式或弹出窗口的问题
$scope.submit= function ( ) {
alertModalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: 'AlertpopupModal.html',
scope: $scope
});
$scope.leave = function () {
console.log("leave");
alertModalInstance.close(true);
};
$scope.cancel = function () {
console.log("cancel");
alertModalInstance.dismiss('cancel');
};
export class AppComponent {
title = 'leave-site';
@HostListener('window:beforeunload', [ '$event' ]) beforeunloadHandler(event) {
return false;
}
}