Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs window.alert() while using mouseEvents causes error

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

like image 790
Ismail Avatar asked Sep 07 '14 15:09

Ismail


People also ask

How do I trigger an alert from an angular component?

Once you've added support for alerts to your app by following the previous steps, you can trigger the display of notifications from any angular component by injecting the alert service and calling one of it's methods for displaying different types of alerts, i.e: success (), error (), info () and warn ().

What is the alertmodule in angular?

The AlertModule ( /src/app/_alert/alert.module.ts) encapsulates the alert component so it can be imported and used by other Angular modules. Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.

What is $window in AngularJS?

A reference to the browser's window object. While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In AngularJS we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.

What are alert notifications in nextnextjs?

Next.js: Next.js Alert notifications are an extremely common requirement in web applications for displaying status messages to the user e.g. error, success, warning and info alerts. In this tutorial we'll cover how to implement a simple reusable alert notification module in Angular 10.


1 Answers

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

like image 188
pankaj Avatar answered Sep 22 '22 14:09

pankaj