Last active
August 29, 2015 14:02
-
-
Save bettysteger/43be83926cab593ca86c to your computer and use it in GitHub Desktop.
Angular Directive for auto focusing an element if a value gets true.
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
/** | |
* Sets focus to this element if the value of focus-me is true. | |
* @example | |
* <a ng-click="addName=true">add name</a> | |
* <input ng-show="addName" type="text" ng-model="name" focus-me="{{addName}}" /> | |
*/ | |
app.directive('focusMe', ['$timeout', function($timeout) { | |
return { | |
scope: { trigger: '@focusMe' }, | |
link: function(scope, element) { | |
scope.$watch('trigger', function(value) { | |
if(value === "true") { | |
$timeout(function() { | |
element[0].focus(); | |
}); | |
} | |
}); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment