<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/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>該示例在輸入框每次獲取焦點(diǎn)時(shí),計(jì)算變量 "count" 會(huì)自動(dòng)加 1。</p>
</body>
</html>測(cè)試看看 ?/?ng-focus 指令用于告訴 AngularJS 在 HTML 元素獲取焦點(diǎn)時(shí)需要執(zhí)行的操作。
ng-focus 指令不會(huì)覆蓋元素的原始 onfocus 事件, 事件觸發(fā)時(shí),ng-focus 表達(dá)式與原始的 onfocus 事件將都會(huì)執(zhí)行。
<element ng-focus="expression"></element>
<a>, <input>, <select>, <textarea>, 和 window 對(duì)象都支持該指令。
| 值 | 描述 |
|---|---|
| expression | 元素獲取焦點(diǎn)時(shí)執(zhí)行的表達(dá)式。 |