event.preventDefault() 不适用于 angularjs 应用程序中的 routeChangeStart [英] event.preventDefault() not working for routeChangeStart in angularjs app
问题描述
希望任何 angularjs 专家都可以帮助我.这是我的 angularjs 代码
Hope any angularjs gurus can help me with this.Here is my angularjs code
$scope.$on('$routeChangeStart', function(event, next, current) {
if ($scope.myForm.$dirty) {
if(!confirm("Unsaved, do u want to continue?")) {
event.preventDefault();
}
}
});
当数据变脏时,它会在浏览器后退按钮单击时发出警报,但在单击取消或确定时,它仍会完成路由更改.似乎 event.preventDefault()
不起作用.任何人都可以指出什么可能是错误的
It alerts in browser back button click when data is dirty, but on clicking cancel or ok it still completes the route change.Seems like event.preventDefault()
is not working.
Can any one point out what may be wrong
推荐答案
我在找到这个方案时遇到了很多麻烦,但是您可以监听$locationChangeStart"事件而不是$routeChangeStart"事件,您可以为此可以防止默认:
I had lots of trouble finding this one, but instead of the "$routeChangeStart" event, you can listen to the "$locationChangeStart" event, for which you can prevent default:
$scope.$on("$locationChangeStart", function(event, next, current) {
if (!confirm("You have unsaved changes, continue navigating to " + next + " ?")) {
event.preventDefault();
}
});
您也可以始终阻止默认值,存储下一个",并显示一个干净的 JS 模式并异步决定.
You could also always prevent default, store "next", and display a clean JS modal and decide asynchronously.
$locationChangeStart 目前未记录,但在此处引用:https://github.com/angular/angular.js/问题/2109
$locationChangeStart is currently undocumented but referenced here : https://github.com/angular/angular.js/issues/2109
这篇关于event.preventDefault() 不适用于 angularjs 应用程序中的 routeChangeStart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!