0

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.

3
  • you can try using the autofocus attribute on the input itself Commented Nov 15, 2016 at 0:47
  • @Ronnie the main problem is i cannot make the changes in html code. Commented Nov 15, 2016 at 17:44
  • what an unfortunate problem. So technically the answer below won't work either. Commented Nov 15, 2016 at 17:54

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.