Javascript angularjs window.alert() 使用 mouseEvents 会导致错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25711829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angularjs window.alert() while using mouseEvents causes error
提问by Ismail
Here is the link to the code:
这是代码的链接:
http://plnkr.co/edit/usrmiNkj5YJY5SlV8ETw?p=preview
http://plnkr.co/edit/usrmiNkj5YJY5SlV8ETw?p=preview
Open up your javascript console and click on "say hi". It will trigger an error that $apply is already in progress.
打开你的 javascript 控制台并点击“say hi”。它将触发 $apply 已经在进行中的错误。
But when you remove this piece of code:
但是当你删除这段代码时:
ng-controller="mouseEvents" ng-mousedown="onMouseDown()" ng-mouseup="onMouseUp()" ng-mousemove="onMouseMove()"
and after saving when you click on "say hi" the error is gone.
保存后,当您单击“打招呼”时,错误消失了。
How can I solve this?
我该如何解决这个问题?
I need the mouseEvents to set flags if the mouse is down or if it is up for multiple different controllers. I can not simply remove it in my code.
我需要 mouseEvents 来设置标志,如果鼠标按下或多个不同的控制器上。我不能简单地在我的代码中删除它。
Edit:
编辑:
Newer angular version solved my issuewithout $timeout v1.3.10 or higher
较新的 angular 版本在没有 $timeout v1.3.10 或更高版本的情况下解决了我的问题
回答by pankaj
Use $timeout to let angular finish dirty checking then show the alert.
使用 $timeout 让 angular 完成脏检查然后显示警报。
app.controller("demoController",function($scope,$window, $timeout){
$scope.save = function(){
$timeout(function(){
window.alert("hi!");
});
};
});
Plunkr: http://plnkr.co/edit/Kxbey5Rc43xsB9v5ugZ5?p=preview
Plunkr:http://plnkr.co/edit/Kxbey5Rc43xsB9v5ugZ5?p=preview

