Skip to content

Instantly share code, notes, and snippets.

@fizerkhan
Forked from mlynch/autofocus.js
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fizerkhan/250a0606ee978bfd33d7 to your computer and use it in GitHub Desktop.
Save fizerkhan/250a0606ee978bfd33d7 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>
*/
angular.module('utils.autofocus', [])
.directive('autofocus', ['$timeout', function($timeout) {
return {
restrict: 'A',
link : function($scope, $element) {
$timeout(function() {
$element[0].focus();
});
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment