【发布时间】:2015-12-09 05:38:24
【问题描述】:
我已经开始使用 Angularjs 并创建了一个小型应用程序。在此应用程序中,我在页面上向用户显示表单。我想在离开页面或刷新页面之前向用户显示确认消息。我为此目的使用以下代码-
$scope.$on('$locationChangeStart', function(event, next, current) {
if(!confirm("The form is dirty, do you want to stay on the page?")) {
event.preventDefault();
}
});
当我使用此代码时,它工作正常,但在控制台面板中引发错误。错误是“错误:$digest 已经在进行中”。
当我搜索此问题的解决方案时,我发现了一些使用 $timeout 的建议。所以,我在我的代码中使用了它,如下所示-
$scope.$on('$locationChangeStart', function(event, next, current) {
$timeout( function() {
if(!confirm("The form is dirty, do you want to stay on the page?")) {
event.preventDefault();
}
});
});
当我使用 $timeout 时,错误会消失,但页面会在确认消息之前离开。
有人知道吗?
【问题讨论】:
-
请创建一个小提琴。我没有收到 $digest 错误消息。
标签: javascript angularjs