test.html
<html>
<head>
<title>获取焦点ng-focus实例</title>
</head>
<body ng-controller="Controller">
<h1>获取焦点ng-focus实例</h1>
<div>演示实例:你可以点击获取焦点,当前焦点在:{{currFocus}}(普通js:target.getAttribute('name'))</div>
<div>演示实例:你可以点击获取焦点,当前焦点在:{{jQCurrFocus}}(jQuery:$(target).attr('name'))</div>
<div>
<ul ng-init="fields = ['name', 'sex', 'age', 'birthday', 'address', 'country']">
<li ng-repeat="field in fields">
<input type="text" name="{{field}}" placeholder="text your {{field}}" ng-focus="getCurrFocus($event.target)" />
</li>
</ul>
</div>
<script src="jquery-1.8.3.js"></script>
<script src="angular1.2.9.js"></script>
<script src="app.js"></script>
</body>
</html>
app.js
var app = angular.module('myModule', []); app.controller('Controller', ['$scope', function($scope) { $scope.getCurrFocus = function(target) { $scope.currFocus = target.getAttribute('name'); $scope.jQCurrFocus = $(target).attr('name'); }; }]); angular.element(document).ready(function() { angular.bootstrap(document,['myModule']); });
运行效果: