Skip to content

Instantly share code, notes, and snippets.

@kevinch
Forked from mlynch/autofocus.js
Last active January 17, 2017 13:49
Show Gist options
  • Save kevinch/e4ce9a666b8c77fe002bed76b7c06c9c to your computer and use it in GitHub Desktop.
Save kevinch/e4ce9a666b8c77fe002bed76b7c06c9c to your computer and use it in GitHub Desktop.
AngularJS Autofocus directive
/**
* the HTML5 autofocus directive from mlynch/autofocus.js (gist.github.com/mlynch/dd407b93ed288d499778) in es6
* I have changed the attribute to auto-focus not to mess with the regular html attribute autofocus
*
* Usage:
* <input type="text" auto-focus>
*
* License: MIT
*/
export function AutofocusDirective($parse, $timeout) {
'ngInject';
return {
restrict: 'A',
link: function($scope, $element) {
$timeout(function() {
$element[0].focus();
});
}
}
};
/**
* Do not forget to import and declare it.
*/
import { AutofocusDirective } from '../app/directives/autofocus.directive';
angular.module('app', [])
.directive('autoFocus', AutofocusDirective)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment