-
-
Save kevinch/e4ce9a666b8c77fe002bed76b7c06c9c 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 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