Skip to content

Instantly share code, notes, and snippets.

@mlynch
Last active August 24, 2022 15:03
Show Gist options
  • Select an option

  • Save mlynch/dd407b93ed288d499778 to your computer and use it in GitHub Desktop.

Select an option

Save mlynch/dd407b93ed288d499778 to your computer and use it in GitHub Desktop.
AngularJS Autofocus directive
/**
* the HTML5 autofocus property can be finicky when it comes to dynamically loaded
* templates and such with AngularJS. Use this simple directive to
* tame this beast once and for all.
*
* Usage:
* <input type="text" autofocus>
*
* License: MIT
*/
angular.module('utils.autofocus', [])
.directive('autofocus', ['$timeout', function($timeout) {
return {
restrict: 'A',
link : function($scope, $element) {
$timeout(function() {
$element[0].focus();
});
}
}
}]);
@mudroljub

Copy link
Copy Markdown

Very clean and cool 👍

@jonkri

jonkri commented Jul 30, 2015

Copy link
Copy Markdown

The lack of an open source license prohibits me from using this. Licensing this under the MIT license (the same license as AngularJS is licensed under) would be great.

@mlynch

mlynch commented Dec 8, 2015

Copy link
Copy Markdown
Author

It's MIT Licensed

@BenjaminConant

Copy link
Copy Markdown

Hey ... just found this after writing my own solution ...

angular.module('finnApp')
  .directive('angularAutoFocus', function () {
    return {
      restrict: 'A',
      link: function (scope, element, attrs) {
        element.focus();
      }
    };
  });

any chance you can explain why you are using element[0] ... instead of just element. Also reasoning for the $timeout .. would prefer to not have it if it is not necessary.

for context I have only tired this on inputs while using uirouter and running into wonkyness with html5 autofocus

@blaise-io

Copy link
Copy Markdown

@BenjaminConant: .focus() (without [0]) only works when you have jQuery loaded. The $timeout is required because at execution time, the element may not be present in the DOM yet.

@eyupaltindal

Copy link
Copy Markdown

thanks 👍

@BenjaminConant

Copy link
Copy Markdown

@blaise-io thanks much!

@ortichon

Copy link
Copy Markdown

Great, thank you

@aarongray

Copy link
Copy Markdown

This directive was working great with an earlier version of Angular 1.4, but I recently upgraded to Angular 1.4.10, and started getting this error: Uncaught TypeError: $exceptionHandler is not a function. The solution was to inject $exceptionHandler in addition to $timeout into the directive. I believe this is because $timeout delegates any exceptions that occur within the function to the $exceptionHandler.

See: https://docs.angularjs.org/api/ng/service/$timeout

@MarcMouallem

Copy link
Copy Markdown

Like a boss!

@iuprade

iuprade commented Sep 6, 2016

Copy link
Copy Markdown

awesome

@DrakeXiang

Copy link
Copy Markdown

The $timeout to the rescue!!!

@sokhomhuy

Copy link
Copy Markdown

awesome

@maxthevoice

Copy link
Copy Markdown

Thanks!

@joryschmidt

Copy link
Copy Markdown

wonderful, thank you sir

@jeponkz

jeponkz commented Sep 18, 2020

Copy link
Copy Markdown

how to add autofocus conditionally? so if expression is true add "autofocus" attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment