Angularjs window.alert () throws error when using mouseEvents

Here is the link to the code:

http://plnkr.co/edit/usrmiNkj5YJY5SlV8ETw?p=preview

Open the javascript console and click "say hi". This will throw an error that $ apply applies.

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 "say hi", the error is gone.

How can I solve this?

I need mouseEvents to set flags if the mouse is not working, or if it is for multiple different controllers. I can't just remove it in my code.

Edit:

New angular version solved my problem without $ timeout v1.3.10 or newer

+4


source to share


2 answers


Use $ timeout to let angular finish the dirty check and then show a warning.

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

  };
}); 

      



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

+6


source


If you want to pass text through a function, this works great:



var app = angular.module('app', []);

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

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
<button ng-click="save('hi')">Hi!</button>
</div>
      

Run codeHide result


0


source







All Articles