Skip to content

Instantly share code, notes, and snippets.

@ezsi
Created September 16, 2014 18:45
Show Gist options
  • Select an option

  • Save ezsi/77a708fde8af5cb4d239 to your computer and use it in GitHub Desktop.

Select an option

Save ezsi/77a708fde8af5cb4d239 to your computer and use it in GitHub Desktop.
AngularJS: directive to auto focus input field when ng-show is triggered
app.directive('showFocus', function($timeout) {
return function(scope, element, attrs) {
scope.$watch(attrs.showFocus,
function (newValue) {
$timeout(function() {
newValue && element.focus();
});
},true);
};
});
Usage: <input type="text" ng-show="customcondition" show-focus="customcondition">
@thethakuri

Copy link
Copy Markdown

You would want to leave the third parameter out of the $watch function as setting it as 'true' will cause unnecessary deep object tree comparison. Find more info here

@jlandry

jlandry commented Jul 14, 2016

Copy link
Copy Markdown

What License can this be used under? I would assume the MIT license but some sort of header or confirmation would be nice.
Thanks.

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