简体   繁体   中英

Set focus to input element angular js

I got a student database with input text element. While the page is loaded a student must be highlighted with input text focussed.

I added a dynamic class that will set the focus if set true. If i map it with a static class it works fine. If i map it with dynamic class, it doesnt.

What is the right way i can pick the dynamic class for the directive.

Here is my JSFIDDLE CODE

Commenting these tds one at a time will help you to understand the issue

<td><input type="text" ng-class="student.focus?'focus-me':''"></td>  //dynamic class
 <td><input type="text" class="focus-me"></td> // static class

This fiddle could help you understand how to properly set a class using ng-class.

<input type="text" ng-class="{'focus-me':student.focus}">

When value of student.focus is true, 'focus-me' class get applied in input tag.

At first try I would simply extend that directive:

  link: function(scope, element,attributes){
    if ($parse(attributes.focusMe)(scope)) {
        element[0].focus();
    }
  }

In html:

<td><input type="text" focus-me="student.focus"></td> 

https://jsfiddle.net/hph1sja2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM