Skip to content

Instantly share code, notes, and snippets.

@bettysteger
Last active August 29, 2015 14:02
Show Gist options
  • Save bettysteger/43be83926cab593ca86c to your computer and use it in GitHub Desktop.
Save bettysteger/43be83926cab593ca86c to your computer and use it in GitHub Desktop.
Angular Directive for auto focusing an element if a value gets true.
/**
* 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