1

I use ng-if to add/remove a typehead input for inline edition purpose. the ng-if value is updated by a "edit" button

So, what is the best or recommended way to give focus to the text input, once it's added to the dom ?

thanks

Lionel

Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
lionelB
  • 209
  • 3
  • 14

2 Answers2

1

The current version of AngularJS doesn't have a focus directive.

But you can refer Input autofocus attribute

And create you own directive for this usage.

Community
  • 1
  • 1
Chen-Tsu Lin
  • 22,876
  • 16
  • 53
  • 63
0

Following Chen-Tsu Lin comment, here is a small directive for focusing element once they're added (used with ng-if)

module.directive('inputFocus',['$timeout', function($timeout){
  'use strict';

  return {
    restrict: 'A',
    link: function(scope, elem){
      $timeout(function(){
        elem[0].focus();
      });
    }
  };
}]);

to use it:

<input type="text" input-focus ng-if="isShown"  />

and here is the JsBin http://jsbin.com/texujona/5/

lionelB
  • 209
  • 3
  • 14