Mouse Event Directives In AngularJS

Introduction

AngularJS has many built-in directives that are used to specify the event. In this article we will discuss about mouse directives. AngularJS has the following directives to support mouse event.

ngMousedown

The ngMouseDown directive allows us to specify the behavior of element(s) on mousedown event. This directive has highest priority.

Syntax

<ANY ELEMENT ng-mousedown="expression">
   ...
</ANY ELEMENT>


Example

HTML

  1. <h4>ngMouseDown Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <button ng-mousedown="count = count + 1" ng-init="count=0">  
  4.         Mouse Down  
  5.     </button>  
  6.     <br /> Count of mouse down on above button: {{count}}  
  7. </div>  
Controller

Here there is no specific item need to done in controller, hence I have defined  blank controller.
  1. var app = angular.module("app", []);  
  2. app.controller("HomeController", function ($scope) {  
  3.   
  4. });  
Output

ngMousedown

ngMouseenter

The ngMouseenter directive allows us to specify the behavior of element(s) on mouse enter event. This directive has highest priority.

Syntax

<ANY ELEMENT ng-mouseenter="expression">
   ...
</ANY ELEMENT>


Example

HTML
  1. <h4>ngMouseEnter Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <div style="width:150px;height:50px;background-color:gray" ng-mouseenter="count = count + 1" ng-init="count=0">  
  4.     </div>  
  5.     <br /> Count of mouse enter on above div object: {{count}}  
  6. </div>  
Controller code is same as above example.

Output

ngMouseenter

ngMouseleave

The ngMouseleave directive allows us to specify the behavior of element(s) on mouse leave event. This directive has highest priority.

Example

HTML
  1. <h4>ngMouseLeave Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <div style="width:150px;height:50px;background-color:gray" ng-mouseleave="count = count + 1" ng-init="count=0">  
  4.     </div>  
  5.     <br /> Count of mouse leave on above div object: {{count}}  
  6. </div>  
Controller code is same as above example.

Output

ngMouseleave

ngMousemove

The ngMousemove directive allows us to specify the behavior of element(s) on mouse move event. This directive has highest priority.

Example

HTML:
  1. <h4>ngMouseMove Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <div style="width:150px;height:50px;background-color:gray" ng-mousemove="x=$event.screenX;y=$event.screenY" ng-init="x=0;y=0;">  
  4.     </div>  
  5.     <br /> X: {{x}}  
  6.     <br /> Y: {{y}}  
  7. </div>  
Controller code is same as above example.

Output

ngMousemove

ngMouseover

The ngMouseover directive allows us to specify the behavior of element(s) on mouse over event. This directive has highest priority.

Example

HTML
  1. <h4>ngMouseOver Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <div style="width:150px;height:50px;background-color:gray" ng-mouseover="count = count + 1;" ng-init="count=0">  
  4.     </div>  
  5.     <br /> Count of mouse over on above div object: {{count}}  
  6. </div>  
Controller code is same as above example.

Output

ngMouseover

ngMouseup

The ngMouseup directive allows us to specify the behavior of element(s) on mouse up event. This directive has highest priority.

Example

HTML
  1. <h4>ngMouseUp Example</h4>  
  2. <div ng-controller="HomeController">  
  3.     <button ng-mouseup="count = count + 1" ng-init="count=0">  
  4.         Mouse Up  
  5.     </button>  
  6.     <br /> Count of mouse Up on above button: {{count}}  
  7. </div>  
Controller code is same as above example.

Output

ngMouseup

Summary

This article is for helping us to learn various directives to support mouse event.

 


Similar Articles