SoFunction
Updated on 2025-04-11

AngularJS basic ng-keydown directive simple example

AngularJS ng-keydown directive

AngularJS instance

Code executed when the key is pressed:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="/libs//1.4.6/"></script>
</head>
<body ng-app="">

<input ng-keydown="count = count + 1" ng-init="count=0" />

<h1>{{count}}</h1>

<p>This instance is pressed every time the key is pressed in the input box,Calculate variables "count" Will automatically add 1。</p>

</body>
</html>

Definition and usage

ng-keydown Directives are used to tell AngularJS what actions are required when pressing a key on a specified HTML element.

ng-keydown Directives will not overwrite the element's native onkeydown event. When the event is triggered, both the ng-keydown expression and the native onkeydown event will be executed.

The sequence of events of keystrokes:




grammar

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

<input>, <select>, <textarea>, and other editable elements support this directive.

Parameter value

 

value describe
expression The expression executed by pressing the key.