angularjs ng-keypress directive

angularjs example

Code executed when the key is pressed:

<! Doctype html>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<body ng-app="">
<input ng-keypress="count=count + 1" ng-init="count=0" />
<p>In this example, every time a key is pressed in the input box,The calculated variable "count" is automatically incremented by one.</p>

Definition and usage

The ng-keypress directive is used to tell angularjs what action is required when pressing a key on the specified html element.

The ng-keypress instruction does not override the element's native onkeypress event. When the event is triggered,Both the ng-keypress expression and the native onkeypress event will be executed.

Sequence of keystrokes:





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

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

Parameter value

value description
expression An expression executed by pressing a key.

  • Previous JavaScript implements copying articles to automatically add copyright
  • Next Using JavaScript to determine whether the mobile browser is in horizontal or vertical screen