AngularJS ng-keypress directive
AngularJS instance
Code executed when the key is pressed:
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body ng-app=""><input ng-keypress="count = count + 1" ng-init="count=0" /><h1>{{count}}</h1><p>This instance will automatically add 1 to the calculation variable "count" every time the key is pressed in the input box. </p></body></html>Definition and usage
The ng-keypress directive tells AngularJS what to do when pressing a key on a specified HTML element.
The ng-keypress directive 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:
1.Keydown
2.Keypress
3.Keyup
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. |