javascript - AngularJS ngShow and focus - Stack Overflow
Use $timeout:
.directive('previewFocus', function($timeout) {
return function(scope, element, attrs) {
scope.$watch('preview',
function (newValue) {
console.log('preview changed!')
$timeout(function() {
newValue && element[0].focus();
}, 0, false);
});
};
});
This works because $timeout defers execution of the code inside $timeout until after the render phase (so after the $watch for ng-show is executed, when the textarea becomes visible)
Side Note: I removed the second argument to your $watch - a deep watch is not necessary for a $watch on a primitive variable.
Demo Fiddle
No comments:
Post a Comment