Aug 3, 2015

Events In Angular js

AngularJS provides event listener directives to handle HTML events like mouse click, double-click, key-press, key-down, text-change events etc.

AngularJS Event Listener Directives

You can attach an event listener to an appropriate HTML element by using one of the following AngularJS event listener directives:
  1. ng-click
  2. ng-dbl-click
  3. ng-keydown
  4. ng-keyup
  5. ng-keypress
  6. ng-change
  7. ng-mousedown
  8. ng-mouseup
  9. ng-mouseenter
  10. ng-mouseleave
  11. ng-mousemove
  12. ng-mouseover

AngularJS Events Example

 

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="app">
<div ng-controller="Ctrl">
  <h2>Click Event</h2>
  <button ng-click="ClickMe()">Click me!</button>
  &nbsp;&nbsp;
  <span>{{ clickCount }}</span>
 
  <h2>Double Click Event</h2>
  <button ng-dblclick="DblClickMe()">Click me!</button>
  &nbsp;&nbsp;
  <span>{{ dblClickCount }}</span>
 
  <h2>Change Event</h2>
  <input type="text" ng-model="FirstName" ng-change="ChangeValue()" />
  &nbsp;&nbsp;
  <span>{{ changeCount }}</span>
 
  <h2>Keyup and Keydown Events</h2>
  <input type="text" ng-model="LastName" ng-keyup="keyUp()" ng-keydown="keyDown()"/>
  &nbsp;&nbsp;
  <span>KeyUp : {{ keyUpCount }}</span>
  <span> & KeyDown : {{ keyDownCount }}</span>

</div>
 
  <script type="text/javascript">
    var app= angular.module("app",[]);
   
    app.controller("Ctrl",function($scope){
      $scope.clickCount=0;
      $scope.dblClickCount=0;
      $scope.changeCount=0;
      $scope.keyUpCount=0;
      $scope.keyDownCount=0;
     
      $scope.ClickMe=function()
      {
        $scope.clickCount +=1;
      }
     
      $scope.DblClickMe=function()
      {
        $scope.dblClickCount +=1;
      }
     
      $scope.ChangeValue=function()
      {
        $scope.changeCount +=1;
      }
     
      $scope.keyUp=function()
      {
        $scope.keyUpCount +=1;
      }
     
      $scope.keyDown=function()
      {
        $scope.keyDownCount +=1;
      }
     
    });
  </script>
</body>
</html>

 

Click Event

   0

Double Click Event

   0

Change Event

   0

Keyup and Keydown Events

   KeyUp : 0 &

 

No comments:

Post a Comment