最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Focus on other input after typeahead-on-select angular-ui-bootstrap - Stack Overflow

programmeradmin0浏览0评论

I have a problem with angular-ui bootstrap: I have 2 input:

<input type="text" ng-model="selected" uib-typeahead="state for state in states | filter:$viewValue | limitTo:8" typeahead-on-select="focusSuccessive($item, $model, $label, $event)" class="form-control">
<textarea class="form-control" id="message" rows="10" data-ng-model="caller.data.text" tabindex="25"></textarea>

And in my controller I have a function focusSuccessive($item, $model, $label, $event)" that should focus textArea after selected.

$scope.focusSuccessive = function($item, $model, $label, $event){
   angular.element("#message").focus();
}

But it doesn't work, But if I put a button like:

<button ng-click="focusSuccessive()">focus</button>

It work, so How Can I focus a textarea or input after typeahead selected?

I have a problem with angular-ui bootstrap: I have 2 input:

<input type="text" ng-model="selected" uib-typeahead="state for state in states | filter:$viewValue | limitTo:8" typeahead-on-select="focusSuccessive($item, $model, $label, $event)" class="form-control">
<textarea class="form-control" id="message" rows="10" data-ng-model="caller.data.text" tabindex="25"></textarea>

And in my controller I have a function focusSuccessive($item, $model, $label, $event)" that should focus textArea after selected.

$scope.focusSuccessive = function($item, $model, $label, $event){
   angular.element("#message").focus();
}

But it doesn't work, But if I put a button like:

<button ng-click="focusSuccessive()">focus</button>

It work, so How Can I focus a textarea or input after typeahead selected?

Share Improve this question edited Jan 31, 2017 at 10:43 LorenzoBerti asked Jan 31, 2017 at 10:34 LorenzoBertiLorenzoBerti 6,97410 gold badges53 silver badges92 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

This behaviour is happening because of callback in typeahead-on-select. It focuses your element but input gets focussed again due to this code

$timeout(function() {
   element[0].focus(); 
 }, 0, false);

You can solve this by focusing your element asynchronously.

In your focusSuccessive function use a $timeout and focus your textarea inside that $timeout

$scope.focusSuccessive = function($item, $model, $label, $event){
  $timeout(function() { 
    document.getElementById('message').focus(); 

  }, 100, false);
}

This will solve your issue.

I am not sure that there is some other way to solve this issue which don't require using $timeout

EDIT

I think there is a option in typeahead-settings

typeahead-focus-on-select

Which is set to true by default, setting it to false will disable this behaviour and you dont need $timeout. Just focus your element normally

<input type="text" ng-model="selected" uib-typeahead="state for state in states | filter:$viewValue | limitTo:8" typeahead-on-select="focusSuccessive($item, $model, $label, $event)" typeahead-focus-on-select=false class="form-control">
发布评论

评论列表(0)

  1. 暂无评论