我不想编写复杂的指令只是为了集中精力。如果该方法使用纯 js 是可以接受的。我的问题是如何捕获 Angular 的 ng-show。
<a ng-click="showInput=true">show</a>
<input type="text" ng-show="showInput" />
上面的代码显示了输入,如何将其聚焦在它的外观上?
最佳答案
您可以添加此指令:
app.directive('showFocus', function($timeout) {
return function(scope, element, attrs) {
scope.$watch(attrs.showFocus,
function (newValue) {
$timeout(function() {
newValue && element.focus();
});
},true);
};
});
并像这样使用它:
<input type="text" ng-show="showInput" show-focus="showInput">
关于javascript - 捕获 ng-show 事件并聚焦输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22776662/