spoorthy chappidi
spoorthy chappidi

Reputation: 11

How to auto focus on first input field using angular

I have 20 html pages in my project I need to implement a common angular code directive to auto focus on the first input field on all pages.

Upvotes: 0

Views: 944

Answers (1)

tommybananas
tommybananas

Reputation: 5736

You can try something like this:

angular.module('utils.autofocus', [])

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

// <input type="text" autofocus>

You could also try ng-autofocus

Upvotes: 1

Related Questions