Skip to content

Instantly share code, notes, and snippets.

@bfillmer
Created August 31, 2014 19:04
Show Gist options
  • Save bfillmer/b66327027b51836f422a to your computer and use it in GitHub Desktop.
Save bfillmer/b66327027b51836f422a to your computer and use it in GitHub Desktop.
Angular JS Focus Directive
(function(){
/**
* Emulates ng-show for focus, i.e.:
*
* <input ng-focus="truthyValue">
*
* Setting truthyValue to true focuses said input.
*/
app.directive('ngFocus', function($timeout){
return {
restrict: 'A',
link: function($scope, $element, $attr){
$scope.$watch($attr.ngFocus, function(value){
if(value){
$timeout(function(){
if($scope.$eval($attr.ngFocus)){
$element[0].focus();
}
}, 0, false);
}
});
}
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment