Skip to content
Related Articles

Related Articles

AngularJS | ng-keydown Directive
  • Last Updated : 26 Mar, 2019

The ng-keydown Directive in AngluarJS is used to apply custom behavior on a keydown event. It is supported by <input>, <select> and <textarea> elements.

Syntax:

 <element ng-keydown="expression"> Contents... </element>

Example: This example uses ng-keydown Directive to change the background color after pressing the button.




<!DOCTYPE html>
<html>
      
<head>
    <title>ng-keydown Directive</title>
      
    <script src=
    </script>
      
    <style type="text/css">
        .keyDown {
            background-color: green;
            color: white;
        }
        .keyUp {
            background-color: white;
        }
    </style>
</head>
  
<body ng-app style="text-align:center">
      
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
      
    <h2>ng-keydown Directive</h2>
      
    <div>
        <b>Enter Name: </b><input type="text"
        ng-model="searchValue" ng-keydown="keyDown=true"
        ng-keyup="keyDown=false" ng-class=
        "{true:'keyDown', false:'keyUp'}[keyDown]" />
          
        <br>
    </div>
</body>
  
</html>

Output:
Before pressing the button:
ngkeydown
After pressing the button:
ngkeydown

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :