在AngularJs中调用$routeChangeStart之前显示确认模式,比如window onbeforeunlo
Show confirmation modal before calling $routeChangeStart in AngularJs like window onbeforeunload
我能够显示一个确认模式,一旦路由开始改变,如果用户选择保持我的路由不改变,它会保持原来的路由,但再次加载表单指令,这导致表单丢失所有的复选框,输入值。它被重置为默认值。
如果用户关闭页面,我可以显示确认模式,表单状态也不会改变。
下面是我的代码:另外,请注意,我也在默认情况下为所有路由注入了一个解析(这个Angular:如何为我的应用程序的所有路由使用一个解析)之后我调用.run()
$rootScope.$on('$routeChangeStart', function (event, newUrl, oldUrl) {
//isLoggedIn also check URL params
if (!Auth.isLoggedIn()) {
$window.location.href = Auth.getLoginUrl();
}
////unsaved modal start
if ($rootScope.unsaved) {
ngDialog.open({
className: 'ngdialog-theme-default unsaved-modal',
template: 'scripts/core/commonmodal/tplUnsavedModal.html',
controller: [function OpenDialogContainer() {
var modal = this;
modal.message = 'You have some unsaved changes. Do you want to leave this page?';
modal.stop = function () {
modal.processing = true;
ngDialog.close();
$rootScope.$broadcast('$routeChangeSuccess');
};
modal.continue = function () {
modal.processing = true;
ngDialog.close();
$rootScope.unsaved = false;
$location.path(newUrl.$$route.originalPath); //Go to page they're interested in
};
}],
controllerAs: 'modal'
});
//prevent navigation by default since we'll handle it
//once the user selects a dialog option
event.preventDefault();
}
});
如果形式不是$pristine
和非$submitted
,则设置$rootScope.unsaved = true
正如你在下面的gifvideo中看到的,在停留时,路由再次运行控制器功能。相反,我想要的是一个类似window onbeforeunload
的效果。
我通过删除这行$rootScope.$broadcast('$routeChangeSuccess');
也修复了它,而不是我做了一个指令。
link: function($scope) {
var message = 'You have some unsaved changes. Do you want to leave this page?';
$window.onbeforeunload = function(){
if ($scope.unsavedChanges) {
return 'You have some unsaved changes. Do you want to leave this page?';
}
};
var $routeChangeStartUnbind = $rootScope.$on('$routeChangeStart', function(event, newUrl) {
if ($scope.unsavedChanges) {
$rootScope.pageloaded = true;//prevent loading icon
ngDialog.open({
appendClassName: 'unsaved-modal',
template: 'scripts/core/commonmodal/tplUnsavedModal.html',
controller: [function OpenDialogContainer() {
var modal = this;
modal.message = message;
modal.stop = function () {
modal.processing = true;
ngDialog.close();
};
modal.continue = function () {
modal.processing = true;
ngDialog.close();
$routeChangeStartUnbind();
$location.path(newUrl.$$route.originalPath); //Go to page they're interested in
$rootScope.pageloaded = false;
$scope.unsavedChanges = false;
$window.location.reload();//$route.reload() does not reload services
};
}],
controllerAs: 'modal'
});
//prevent navigation by default since we'll handle it
//once the user selects a dialog option
event.preventDefault();
}
});
$scope.$on('$destroy', function() {
window.onbeforeunload = null;
$routeChangeStartUnbind();
});
}
相关文章:
- 弹出格式化的模式对话框,并在用户确认时转发
- 模式警报,但确认按钮未对脚本做出反应
- 确认删除的模式对话框
- Bootstrap3确认模式回调触发两次
- 使用弹出模式中断k-pager-nav以确认/拒绝移动到新页面
- 确认脏表单上的角度模式关闭
- 如何使用 jquery 将变量传递给引导确认模式按钮
- 简单模态 - 如何将电子邮件地址传递给确认模式
- 引导模式确认
- jQuery UI 模式确认对话框,ASP.NET 不会在单击事件中触发
- 用户不会显示 JQuery 模式对话框确认
- 在引导确认模式下控制确认按钮
- 如何在asp.net MVC中创建确认框(模式弹出)
- 使用引导程序验证程序进行验证后,停止表单提交并打开引导模式(确认对话框)
- 确认模式按钮 asp.net 的操作
- 如何跳过确认弹出前卸载和显示我自己的引导模式
- 删除确认 - 打开模式弹出窗口时从 html 传递数据
- Bootstrap 3:添加确认模式框jQuery内联删除行
- 简单确认模式/弹出式-MVC 5/Bootstrap 3
- 如何在jQuery模式确认对话框之后提交表单