事件。preventDefault()在angularjs应用程序不工作的routeChangeStart [英] event.preventDefault() not working for routeChangeStart in angularjs app
问题描述
希望任何angularjs大师可以帮我this.Here是我angularjs code
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();
}
}
});
这提醒在浏览器后退按钮的点击数据时是脏的,但是点击取消或确定它依然完成路由change.Seems像事件。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
推荐答案
我有很多的麻烦找到这一个,但不是在$ routeChangeStart事件,您可以聆听到$ locationChangeStart事件,您可以prevent默认值:
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();
}
});
您也可以随时prevent默认情况下,存储下一步,并显示一个干净的JS模式和异步决定。
You could also always prevent default, store "next", and display a clean JS modal and decide asynchronously.
$ locationChangeStart目前无证,但此处引用:<一href=\"https://github.com/angular/angular.js/issues/2109\">https://github.com/angular/angular.js/issues/2109
$locationChangeStart is currently undocumented but referenced here : https://github.com/angular/angular.js/issues/2109
这篇关于事件。preventDefault()在angularjs应用程序不工作的routeChangeStart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!