Focus on other input after typeahead-on-select angular-ui-bootstrap

孤者浪人 提交于 2021-02-10 16:22:05

问题


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?


回答1:


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">


来源:https://stackoverflow.com/questions/41955332/focus-on-other-input-after-typeahead-on-select-angular-ui-bootstrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!