This question bears a striking resemblance to AngularJS - Focusing an input element when a checkbox is clicked. Here, I present a nearly identical solution: the focus
directive with the attribute editing
:
app.directive('focus', function() {
return function(scope, element, attrs) {
scope.$watch(attrs.focus, function(newValue) {
newValue && element[0].focus()
})
}
});
@Josh has previously mentioned that we can utilize the native DOM focus() method by referencing the raw DOM element through element[0] — as element itself represents a wrapped jqLite (or jQuery if loaded) element.
The HTML:
<input ng-show="editing" type="text" ng-model="text" value="{{text}}"
focus="editing">
Fiddle.