SoFunction
Updated on 2025-04-08

AngularJS basic ng-keypress directive simple example

AngularJS ng-keypress 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-keypress="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-keypressDirectives are used to tell AngularJS what actions are required when pressing a key on a specified HTML element.

ng-keypressDirectives will not overwrite the element's native onkeypress event. When the event is triggered, both the ng-keypress expression and the native onkeypress event will be executed.

The sequence of events of keystrokes:




grammar

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

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

Parameter value

value describe
expression The expression executed by pressing the key.