Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set element focus in angular way

After looking for examples of how set focus elements with angular, I saw that most of them use some variable to watch for then set focus, and most of them use one different variable for each field they want to set focus. In a form, with a lot of fields, that implies in a lot of different variables.

With jquery way in mind, but wanting to do that in angular way, I made a solution that we set focus in any function using the element's id, so, as I am very new in angular, I'd like to get some opinions if that way is right, have problems, whatever, anything that could help me do this the better way in angular.

Basically, I create a directive that watch a scope value defined by the user with directive, or the default's focusElement, and when that value is the same as the element's id, that element set focus itself.

angular.module('appnamehere')   .directive('myFocus', function () {     return {       restrict: 'A',       link: function postLink(scope, element, attrs) {         if (attrs.myFocus == "") {           attrs.myFocus = "focusElement";         }         scope.$watch(attrs.myFocus, function(value) {           if(value == attrs.id) {             element[0].focus();           }         });         element.on("blur", function() {           scope[attrs.myFocus] = "";           scope.$apply();         })               }     };   }); 

An input that needs to get focus by some reason, will do this way

<input my-focus id="input1" type="text" /> 

Here any element to set focus:

<a href="" ng-click="clickButton()" >Set focus</a> 

And the example function that set focus:

$scope.clickButton = function() {     $scope.focusElement = "input1"; } 

Is that a good solution in angular? Does it have problems that with my poor experience I don't see yet?

like image 354
Tiago Zortéa De Conto Avatar asked Aug 31 '14 21:08

Tiago Zortéa De Conto


People also ask

How do you set the focus on an element?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How do I set focus on Ng select?

Go to 'ng-select component and search something you get some option regarding it. ' Click OR select the option. And after the selection the value Cursor focus out from the component.

How do you know if an element is focused in angular?

hasFocus() : whether the document or any element inside the document has focus. document. activeElement : Property containing which element currently has focus.


1 Answers

The problem with your solution is that it does not work well when tied down to other directives that creates a new scope, e.g. ng-repeat. A better solution would be to simply create a service function that enables you to focus elements imperatively within your controllers or to focus elements declaratively in the html.

DEMO

JAVASCRIPT

Service

 .factory('focus', function($timeout, $window) {     return function(id) {       // timeout makes sure that it is invoked after any other event has been triggered.       // e.g. click events that need to run before the focus or       // inputs elements that are in a disabled state but are enabled when those events       // are triggered.       $timeout(function() {         var element = $window.document.getElementById(id);         if(element)           element.focus();       });     };   }); 

Directive

  .directive('eventFocus', function(focus) {     return function(scope, elem, attr) {       elem.on(attr.eventFocus, function() {         focus(attr.eventFocusId);       });        // Removes bound events in the element itself       // when the scope is destroyed       scope.$on('$destroy', function() {         elem.off(attr.eventFocus);       });     };   }); 

Controller

.controller('Ctrl', function($scope, focus) {     $scope.doSomething = function() {       // do something awesome       focus('email');     };   }); 

HTML

<input type="email" id="email" class="form-control"> <button event-focus="click" event-focus-id="email">Declarative Focus</button> <button ng-click="doSomething()">Imperative Focus</button> 
like image 174
ryeballar Avatar answered Sep 28 '22 16:09

ryeballar