I have an iOS app using angularJS and ionic. I am trying to get the keyboard focus on to the next input field when the 'Return' button is hit. Here is the HTML code where the ng-repeat exists:
<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>
You can see in my input element I have "on-return="nextLine($index)"". There I am using my custom directive which is used when the return button is hit:
.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();
}
});
};
});
Which calls my nextLine function in my controller:
myScope.nextLine = function (index) {
//get next html input element and .setFocus()
}
Where index is the current input box index. I need a way to get the next HTML input element and set the focus there. Any help is appreciated.
scope.$apply
directly, instead wrap in$timeout()
- it does a "smart" use of$apply
- not a "brutal" one. – DotBot