For those utilizing angularjs 1.2, there are two directives available to assist in monitoring if a field has focus: ng-focus and ng-blur. If these are not applicable, it is relatively easy to create custom directives. View the code snippet on (plunker):
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="jquery" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b5a555c4e575a491551487b0a15091543">[email protected]</a>" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js" data-semver="1.2.16"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<form>
<input ng-model="name" id="name" ng-focus="focused()" ng-blur="blurred()">
<input ng-model="name2">
</form>
</body>
</html>
The corresponding javascript portion:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.focused = function() {
console.log("got focus");
}
$scope.blurred = function() {
console.log("lost focus");
}
});
If validation is the primary concern, consider exploring form validation in angularjs, specifically using something like myForm.myInput.$valid. Angularjs automatically assigns the ng-valid and ng-invalid classes based on validation status.