【发布时间】:2016-12-24 11:37:12
【问题描述】:
我有一个使用 angularJS 和 ionic 的 iOS 应用程序。当按下“返回”按钮时,我试图将键盘焦点放在下一个输入字段上。这是存在 ng-repeat 的 HTML 代码:
<div class="row item-list" ng-repeat="item in shoppingList">
<div class="col col-10 col-check" ng-click="checkItem($index)">
<i class="icon ion-checkmark-round" ng-if="item.Action == 'check'"></i>
</div>
<div class="col col-memo" ng-click="updateMemo($index)">
<label class="item-list item-input">
<input id="inputText" type="text" placeholder="" ng-model="item.EventContent" on-return="nextLine($index)"/>
</label>
</div>
</div>
您可以在我的输入元素中看到“on-return="nextLine($index)"”。我正在使用我的自定义指令,该指令在按下返回按钮时使用:
.directive('onReturn', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if (event.which === 13) {
scope.$apply(function () {
scope.$eval(attrs.onReturn);
});
event.preventDefault();
}
});
};
});
在我的控制器中调用我的 nextLine 函数:
myScope.nextLine = function (index) {
//get next html input element and .setFocus()
}
其中 index 是当前输入框的索引。我需要一种方法来获取下一个 HTML 输入元素并在那里设置焦点。任何帮助表示赞赏。
【问题讨论】:
-
不要直接使用
scope.$apply,而是使用$timeout()- 它“聪明”地使用$apply- 而不是“残酷”的。
标签: javascript html ios angularjs