我不想编写复杂的指令来集中注意力。如果方法使用纯js,那么这是可以接受的。我的问题是如何捕捉角度的ng显示。捕获展示事件和焦点输入字段
<a ng-click="showInput=true">show</a>
<input type="text" ng-show="showInput" />
上面的代码显示输入,如何将它集中在它的外观?
我不想编写复杂的指令来集中注意力。如果方法使用纯js,那么这是可以接受的。我的问题是如何捕捉角度的ng显示。捕获展示事件和焦点输入字段
<a ng-click="showInput=true">show</a>
<input type="text" ng-show="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>
不起作用,它是有道理的,但只是不起作用。找不到错误。 – user3398172
如果不起作用,则不应将其标记为答案。 – DynamiteReed
您可以添加此指令:
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">
不确定是否需要您的项目,但请记住,您无法以编程方式将'focus'触发到iOS设备上的元素上(必须通过点击/点按操作),但是您可以遍历元素第一个重点是设置http://stackoverflow.com/questions/18728166/programatically-focus-on-next-input-field-in-mobile-safari – haxxxton