5

Here is the link to the code:

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.

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.

Edit:

Newer angular version solved my issue without $timeout v1.3.10 or higher

Ismail
  • 8,904
  • 3
  • 21
  • 39

1 Answers1

8

Use $timeout to let angular finish dirty checking then show the alert.

app.controller("demoController",function($scope,$window, $timeout){
  $scope.save = function(){
    $timeout(function(){
       window.alert("hi!");
    });

  };
}); 

Plunkr: http://plnkr.co/edit/Kxbey5Rc43xsB9v5ugZ5?p=preview

pankaj
  • 474
  • 5
  • 15
  • It works! Can you give an link or explain more deeply about why it works and why I had the error? Did you see the error too before using $timeout? – Ismail Sep 07 '14 at 19:17
  • I don't think it is an good practice to use $timeout each time I want to use window.alert() – Ismail Sep 07 '14 at 19:30
  • This is because you have trigged movedown/up event on click. Whenever a alert box is displayed browser stops the further execution of javascript until user responds, if angular dirty checking is in progress then it gives you error. I assume you don't want show alert on production then $timeout won't be needed. – pankaj Sep 07 '14 at 19:38
  • 1
    Use console.log in place of window.alert for debugging purpose. It won't require $timeout, and also a good practice to follow. – pankaj Sep 07 '14 at 19:40
  • it is not for debugging purposes, I will use it in production to. There must be some kind of solution or someone else who has encountered the same problem – Ismail Sep 08 '14 at 10:34