Created
September 16, 2014 18:45
-
-
Save ezsi/77a708fde8af5cb4d239 to your computer and use it in GitHub Desktop.
AngularJS: directive to auto focus input field when ng-show is triggered
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
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"> |
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
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