如何聚焦angularjs的ui选择输入

how to focus angularjs ui-select input?

本文关键字:ui 选择 输入 angularjs 何聚焦 聚焦      更新时间:2023-09-26

我的问题是我有一个框模式,有ui-选择下拉。当模态出现时,我想让myUser可以选择一个带有键盘的项目。但重点不在于模态。我如何专注于ui-select的输入(重要的,输入)?谢谢。

<ui-select class="cursorISI aaa  selectType2 border-fixed" 
    on-select="Func.selectAnotherProject($item, $model)" theme="selectize"
    ng-model="oldSelectedProject"> <ui-select-match>{{$select.selected.title}}
</ui-select-match> <ui-select-choices
    repeat="project in  projectList|filter: $select.search "
    refresh-delay="0" style="direction: ltr; text-align: right;">
<div ng-bind="project.title"
    ng-show="runSelectedProject.uid!=project.uid"></div>
</ui-select-choices> </ui-select>

其实很简单。看一下文档就知道了。

autofocus:加载时自动对焦。默认值为"false"

把它加到html的第一行比如<ui-select class="cursorISI aaa selectType2 border-fixed" autofocus="true"

希望这对你有帮助

如果您想关注需求,那么ui-select也提供事件广播支持。您需要将广播事件名称放在ui-selectfocus-on属性中,并通过$scope.$broadcast(...)广播该事件

<ui-select focus-on="UiSelectDemo1" ng-model="ctrl.person.selected" theme="select2" ng-disabled="ctrl.disabled" style="min-width: 300px;" title="Choose a person">
    <ui-select-match placeholder="Select a person in the list or search his name/age...">{{$select.selected.name}}</ui-select-match>
    <ui-select-choices repeat="person in ctrl.people | propsFilter: {name: $select.search, age: $select.search}">
        <div ng-bind-html="person.name | highlight: $select.search"></div>
          <small>
            email: {{person.email}}
            age: <span ng-bind-html="''+person.age | highlight: $select.search"></span>
          </small>
    </ui-select-choices>
</ui-select>

调用$scope.$broadcast('UiSelectDemo1');按钮点击或任何其他触发/事件。

如果你想让ui-select在初始化后聚焦并打开:

yourModule.directive('uiSelectOpened', function($timeout) {
    return {
        restrict: 'A',
        require: 'uiSelect',
        link: function(scope, element, attrs, uiSelect) {
            $timeout(()=> uiSelect.toggle())
        }
    };
});
html:

<ui-select ... autofocus="true" ui-select-opened>...</ui-select>

我有同样的问题,需要添加一个指令

.directive('focusMe', function($timeout) {
    return {
      scope: {trigger: '@focusMe'},
      link: function(scope, element) {
        scope.$watch('trigger', function(value) {
          if (value === 'true') {
            $timeout(function() {
              element[0].focus();
            });
          }
        });
      }
    };
  })

然后你的ui-select add

 focus-me="true"

如果你想从控制器或指令中实现它(这个方法是独立工作的,它完全是角化的方式)-

说ui-select被包装在div-

<div id="ui-select-wrapper">
    <ui-select>
        ...
    </ui-select>
</div>

setFocus的控制器方法-

var uiSelectWrapper = document.getElementById('ui-select-wrapper');
var focusser = uiSelectWrapper.querySelector('.ui-select-focusser');
var focusser = angular.element(uiSelectWrapper.querySelector('.ui-select-focusser'));
focusser.focus();

使用内置焦点选项!在控制器中,将此代码放入所需的方法中。

            if(yourform.$error.required[0].$$attr.focusOn){
                $scope.$broadcast(yourform.$error.required[0].$$attr.focusOn);
            }

同时为ui-select

定义焦点道具
focus-on="UiSelectDemo1"