Skip to content

Instantly share code, notes, and snippets.

@andyshora
Created June 24, 2014 08:57
Show Gist options
  • Save andyshora/99be873de7d3350317ab to your computer and use it in GitHub Desktop.
Save andyshora/99be873de7d3350317ab to your computer and use it in GitHub Desktop.
Input focus dynamically as an AngularJS directive
angular.module('ng').directive('ngFocus', function($timeout) {
return {
link: function ( scope, element, attrs ) {
scope.$watch( attrs.ngFocus, function ( val ) {
if ( angular.isDefined( val ) && val ) {
$timeout( function () { element[0].focus(); } );
}
}, true);
element.bind('blur', function () {
if ( angular.isDefined( attrs.ngFocusLost ) ) {
scope.$apply( attrs.ngFocusLost );
}
});
}
};
});
@andyshora
Copy link
Author

@andyshora
Copy link
Author

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