AngularJS Event Handling

When we want to create advanced AngularJS applications such as User Interaction Forms, then we need to handle DOM events like mouse clicks, moves, keyboard presses, change events and so on. AngularJS has a simple model for how to add event listeners. We can attach an event listener to an HTML element using one of the following AngularJS event listener directives:

  • ng-click
  • ng-dbl-click
  • ng-mousedown
  • ng-mouseup
  • ng-mouseenter
  • ng-mouseleave
  • ng-mousemove
  • ng-mouseover
  • ng-keydown
  • ng-keyup
  • ng-keypress
  • ng-change

Here is a simple AngularJS event listener directive example:

  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Acgular Event</title>  
  11.     <script src="~/Scripts/angular.js"></script>  
  12.   
  13.     <script>  
  14.         angular.module("myapp", [])  
  15.                 .controller("Controller1", function ($scope) {  
  16.                     $scope.myData = {};  
  17.                     $scope.myData.dvClick = function () {  
  18.                         alert("Div clicked");  
  19.                     }  
  20.                 });  
  21.   
  22.     </script>  
  23. </head>  
  24. <body ng-app="myapp">  
  25.     <div ng-controller="Controller1">  
  26.         <div ng-click="myData.dvClick()">Click here</div>  
  27.     </div>  
  28. </body>  
  29. </html>  

When we click the text within the div, the myData.dvClick() function will be called. As you can see in the controller function, the myData object has a dvClick() function added to it. The event listener functions called are functions added to the $scope object by the controller function.

The event listener directives have a special variable named $event that we can use as a parameter to the event listener function. The $event variable contains the original browser event object. Here is an example:

  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Acgular Event</title>  
  11.     <script src="~/Scripts/angular.js"></script>  
  12.   
  13.     <script>  
  14.         angular.module("myapp", []).controller("Controller1", function ($scope) {  
  15.             $scope.myData = {};  
  16.             $scope.myData.dvClick = function (event) {  
  17.                 alert("clicked: " + event.clientX + ", " + event.clientY);  
  18.             }  
  19.         });  
  20.     </script>  
  21. </head>  
  22. <body ng-app="myapp">  
  23.     <div ng-controller="Controller1">  
  24.         <div ng-click="myData.dvClick($event)">Click here With Event</div>  
  25.     </div>  
  26. </body>  
  27. </html>  

We can also other parameters to our event listener functions. Here is an example that adds an event listener function to a list of li elements:

  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Acgular Event</title>  
  11.     <script src="~/Scripts/angular.js"></script>  
  12.   
  13.     <script>  
  14.         angular.module("myapp", [])  
  15.              .controller("Controller1", function ($scope) {  
  16.                  $scope.myData = {};  
  17.                  $scope.myData.items = [{ CName: "Smith" }, { CName: "John" }, { CName: "Tony" }];  
  18.   
  19.                  $scope.myData.dvClick = function (item, event) {  
  20.                      alert("clicked on : " + item.CName + " + " + event.clientX + ": " + event.clientY);  
  21.                  }  
  22.              });  
  23.     </script>  
  24. </head>  
  25. <body ng-app="myapp">  
  26.     <div ng-controller="Controller1">  
  27.         <ul>  
  28.             <li ng-repeat="item in myData.items"  
  29.                 ng-click="myData.dvClick(item, $event)">{{item.CName}}</li>  
  30.         </ul>  
  31.     </div>  
  32. </body>  
  33. </html>