Last active
August 24, 2022 15:03
-
-
Save mlynch/dd407b93ed288d499778 to your computer and use it in GitHub Desktop.
AngularJS Autofocus directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
}); | |
} | |
} | |
}]); |
awesome
The $timeout
to the rescue!!!
awesome
Thanks!
wonderful, thank you sir
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
Like a boss!