[英]catch ng-show event and focus input field
我不想写复杂的指令只是为了集中注意力。 如果方法将使用纯js,则可以接受。 我的问题是如何捕捉角度的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">
你可以尝试使用像这样的简单指令:
app.directive('FocusOnVisibility', function () {
return function (scope, element, attrs) {
//Watch the showInput model
scope.$watch('showInput', function () {
//If the model changes to true, focus on the element
if (scope.showInput === true) {
//Assumes that the element has the focus method
//If not, then you can have your own logic to focus here
element.focus();
}
});
};
});
然后,您可以在标记中使用此指令:
<input type="text" ng-show="showInput" focus-on-visibility>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.