Home>

angularjs ng-focus directive

angularjs example

Execute the expression when the input box gets focus:

<! 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-focus="count=count + 1" ng-init="count=0" />
<h1>{{count}}</h1>
<p>This instance every time the input box gets focus,The calculated variable "count" is automatically incremented by one.</p>
</body>
</html>

In this instance, every time the input box gets focus,The calculated variable "count" is automatically incremented by one.

Definition and usage

The ng-focus directive is used to tell angularjs what to do when the html element gets focus.

The ng-focus directive does not override the element's original onfocus event. When the event is triggered,Both the ng-focus expression and the original onfocus event will be executed.

grammar

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

<a> ;,<input> ;,<select> ;,<textarea> ;, and window objects all support this directive.

Parameter value

value description
expression The expression that is executed when the element gets focus.

  • Previous How does JSP get client real IP address
  • Next Source code interpretation browser compatible module support in jQ