by using document.activeElement in JavaScript, we can get element with focus. AngularJS is providing $document for window.document. Here I have written one example, Find below for it

Markup

Here I have given two input fields with names. Whenever user clicks on those input fields the focused element name will change. Here I have used both window.document and $document also
<!DOCTYPE html>
<html ng-app="" ng-controller="myCtrl">
<body ng-click="updateFocusElement()">
<div>
item 1 : <input name="item1"/><br/><br/>
item 2 : <input name="item2"/><br/><br/>
<b>Focused Item : (with document)</b> {{fucusedItemName}}<br/><br/>
<b>Focused Item : (with $document)</b> {{fucusedItemName1}}<br/><br/>
</div>
<script>
function myCtrl($scope, $document) {
    
    $scope.updateFocusElement = function() {
       
        $scope.fucusedItemName = angular.element(window.document.activeElement).attr('name');
        $scope.fucusedItemName1 = angular.element($document[0].activeElement).attr('name');
       
    };

}
</script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>

0 comments:

Blogroll

Follow this blog by Email

Popular Posts