AngularJS includes certain directives which can be used to provide custom behavior on various DOM events, such as click, dblclick etc.

Let’s take a look at some of the important event directives of AngularJS.

ng-click

The ng-click directive is used to provide an event handler for click event.
  1. <!DOCTYPE html>
  2. <html ng-app="mainApp">
  3. <head>
  4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  5. <title></title>
  6. <meta charset="utf-8" />
  7. <script>
  8. var mainApp = angular.module("mainApp", []);
  9. mainApp.controller("appController", function ($scope) {
  10. $scope.btn_click = function(name){
  11. alert(name);
  12. }
  13. });
  14. </script>
  15. </head>
  16. <body ng-controller="appController">
  17. Enter your name : <input type="text" ng-model="name" />
  18. <input type="button" value="click me" ng-click="btn_click(name)" />
  19. </body>
  20. </html>

Output

Angular

ng-dblclick (Double Click)

The ng-dblclick directive is used to provide an event handler for a double-click event.

In this example, we will use the same example which we’ve used for ng-click. The only difference is that we are going to use ng-dblclick instead of using ng-click. It also means that the below code will only work if user hits double click.

  1. <!DOCTYPE html>
  2. <html ng-app="mainApp">
  3. <head>
  4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  5. <title></title>
  6. <meta charset="utf-8" />
  7. <script>
  8. var mainApp = angular.module("mainApp", []);
  9. mainApp.controller("appController", function ($scope) {
  10. $scope.dblbtn_click = function (name) {
  11. alert(name);
  12. }
  13. });
  14. </script>
  15. </head>
  16. <body ng-controller="appController">
  17. Enter your name : <input type="text" ng-model="name" />
  18. <input type="button" value="click me" ng-dblclick="dblbtn_click(name)" />
  19. </body>
  20. </html>

Output
Angular
ng-mouseenter and ng-mouseleave

In the below example, the ng-class directive includes map of the CSS classes, so GreenDiv will be applied if enter=true and RedDiv will applied if leave=false.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
  5. <style>
  6. .GreenDiv {
  7. width: 100px;
  8. height: 100px;
  9. background-color: green;
  10. border-radius: 2px 2px 2px 2px;
  11. padding: 2px 2px 2px 2px;
  12. }
  13. .RedDiv {
  14. width: 100px;
  15. height: 100px;
  16. background-color: red;
  17. border-radius: 2px 2px 2px 2px;
  18. padding: 2px 2px 2px 2px;
  19. }
  20. </style>
  21. </head>
  22. <body ng-app>
  23. <div ng-class="{RedDiv: enter, GreenDiv: leave}" ng-mouseenter="enter=true;leave=false;" ng-mouseleave="leave=true;enter=false">
  24. Mouse <span ng-show="enter">Enter</span> <span ng-show="leave">Leave</span>
  25. </div>
  26. </body>
  27. </html>

Output

Angular

ng-change (mouse event)

The below example demonstrates how to use ng-change event on a drop-down list change event.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title> </title>
  5. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  6. <script type="text/javascript">
  7. var app = angular.module('myApp', []);
  8. app.controller('MyCtrl', function($scope) {
  9. $scope.arrlist = [{
  10. "userid": 1,
  11. "name": "Tim"
  12. }, {
  13. "userid": 2,
  14. "name": "Riya"
  15. }, {
  16. "userid": 3,
  17. "name": "Sam"
  18. }];
  19. $scope.getdetails = function() {
  20. if ($scope.userselected.userid == "1") {
  21. $scope.result = true;
  22. $scope.result1 = false;
  23. $scope.result2 = false;
  24. } else if ($scope.userselected.userid == "2") {
  25. $scope.result = false;
  26. $scope.result1 = true;
  27. $scope.result2 = false;
  28. } else if ($scope.userselected.userid == "3") {
  29. $scope.result = false;
  30. $scope.result1 = false;
  31. $scope.result2 = true;
  32. } else {
  33. $scope.result = false;
  34. $scope.result1 = false;
  35. $scope.result2 = false;
  36. }
  37. }
  38. });
  39. </script>
  40. </head>
  41. <body>
  42. <div ng-app="myApp" ng-controller="MyCtrl"> <select name="users" ng-options="user.name for user in arrlist" ng-change="getdetails()" ng-model="userselected">
  43. <option value="">--Select Name--</option>
  44. </select><br /><br />
  45. <div style="padding:10px; border:1px solid red; width:30%; font-weight:bold" ng-show='result'>Hello Tim</div>
  46. <div style="padding:10px; border:1px solid yellow; width:30%; font-weight:bold" ng-show='result1'>Hello Riya</div>
  47. <div style="padding:10px; border:1px solid green; width:30%; font-weight:bold" ng-show='result2'>Hello Sam</div>
  48. </div>
  49. </body>
  50. </html>
Output

Angular

ng-mousemove (mouse event)

The following example demonstrates how to use ng- mousemove event.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>
  5. </title>
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  7. <script type="text/javascript">
  8. var app = angular.module('myApp', []);
  9. app.controller('MyCtrl', function ($scope) {
  10. $scope.count = 0;
  11. $scope.getdetails = function () {
  12. $scope.count = $scope.count + 1;
  13. }
  14. });
  15. </script>
  16. </head>
  17. <body>
  18. <div ng-app="myApp" ng-controller="MyCtrl">
  19. <div style="padding:10px; border:1px solid black; background-color:aqua; width:200px; height:100px; cursor:pointer;" ng-mousemove="getdetails()">Move Mouse Cursor Here</div><br /><br />
  20. <span style="color:Red; margin-top:25px;">Number of Times Mouse Move Event Fired: {{count}}</span>
  21. </div>
  22. </body>
  23. </html>

Output

Angular

ng-keydown, ng-keyup and ng-keypress (mouse event)

The below example demonstrates how to use ngkey-down, ngkeyup, and ng-keypress events.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>
  5. </title>
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  7. <script type="text/javascript">
  8. var app = angular.module('myApp', []);
  9. app.controller('MyCtrl', function ($scope) {
  10. $scope.getkeys = function (event) {
  11. $scope.keyval = event.keyCode;
  12. }
  13. });
  14. app.controller('MyCtrl1', function ($scope) {
  15. $scope.getkeys = function (event) {
  16. $scope.keyval = event.keyCode;
  17. }
  18. });
  19. app.controller('MyCtrl2', function ($scope) {
  20. $scope.getkeys = function (event) {
  21. $scope.keyval = event.keyCode;
  22. }
  23. });
  24. </script>
  25. </head>
  26. <body ng-app="myApp" ng-controller="MyCtrl">
  27. <div ng-controller="MyCtrl">
  28. <h3>ng-keydown</h3>
  29. Enter any text: <input type="text" ng-keydown="getkeys($event)" ng-model="txt"><br /><br />
  30. <span style="color:green">Last Key Code: {{keyval}}</span>
  31. </div>
  32. <hr />
  33. <div ng-controller="MyCtrl">
  34. <h3>ng-keyup</h3>
  35. Enter any text: <input type="text" ng-keyup="getkeys($event)" ng-model="txt"><br /><br />
  36. <span style="color:red">Last Key Code: {{keyval}}</span>
  37. </div>
  38. <hr />
  39. <div ng-controller="MyCtrl2">
  40. <h3>ng-keypress</h3>
  41. Enter any text: <input type="text" ng-keypress="getkeys($event)" ng-model="txt"><br /><br />
  42. <span style="color:yellowgreen">Last Key Code: {{keyval}}</span>
  43. </div>
  44. </body>
  45. </html>

Output

Angular