如何检测 Angular 1 上的焦点

How to detect focus on Angular 1

我在表单和 ng-focus 上有多个输入文本,我正在调用一个方法,比如说 GetFieldName()。 我的 angular.js 我如何才能检测到将焦点放在第一个字段或第二个字段上的方法。 我如何验证使用 getFieldName() 方法来获取关注它的字段。

最好的解决方案是使用 angularJS 指令来获取属性 或进行验证 https://docs.angularjs.org/guide/forms

这是使用控制器和 $event 从元素获取名称属性的解决方案

var myApp = angular.module('myApp',[]);
myApp.controller('formCtrl', ['$scope', function($scope) {
  $scope.text = "sample text";
  $scope.getName = function(event){
   $scope.text = event.target.getAttribute('name');
  };
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div  ng-app="myApp">
<form ng-controller="formCtrl">
{{text }}
<input type="text" name="text1" ng-focus="getName($event)">
<input type="text" name="text2" ng-focus="getName($event)">
<input type="text" name="text3" ng-focus="getName($event)">
<input type="text" name="text4" ng-focus="getName($event)">

</form>
</div>