如何在(AngularJS)ui-select组件中设置搜索输入的最大长度?

lea*_*iro 4 html javascript jquery angularjs ui-select

假设我有一个ui-select的以下(非常基本的)代码

<ui-select ng-model="vm.selected">
    <ui-select-match>
        <span ng-bind="$select.selected.label"></span>
    </ui-select-match>
    <ui-select-choices repeat="item in vm.items">
        <span ng-bind="item.label"></span>
    </ui-select-choices>
</ui-select>
Run Code Online (Sandbox Code Playgroud)

现在,这会生成所有html节点等,其中包含用于搜索和过滤列表中显示的选项的输入.

问题是:

如何设置(在任何变体中)输入搜索的最大长度?

指令不提供任何内置数据属性.

因此,预期的行为是:如果我设置最大长度为10个字符,当用户键入/复制+粘贴大于指定长度的字符串时,输入搜索中的字符串会被截断(但是,如果你能提供给我的话)与其他一些解决方案,允许用户在输入搜索中只输入一定数量的字符,我真的很感激)

在SO上找到了这个相关的问题,但它并不适用于这种情况,因为我无法通过ng-model或类似的方式访问输入搜索中输入的值.

The*_*ear 6

您可以向指令添加自定义属性指令ui-select,然后在其中搜索input.ui-select-search,最后添加和HTML maxlength属性...(PLUNKER)

HTML

<ui-select ng-model="vm.selected" maxlen="15">
    <ui-select-match>
        <span ng-bind="$select.selected.label"></span>
    </ui-select-match>
    <ui-select-choices repeat="item in vm.items">
        <span ng-bind="item.label"></span>
    </ui-select-choices>
</ui-select>
Run Code Online (Sandbox Code Playgroud)

指示

app.directive('maxlen', function() {
  return {
    restrict: 'A',
    link: function(scope, element, attr) {
      var $uiSelect = angular.element(element[0].querySelector('.ui-select-search'));
      $uiSelect.attr("maxlength", attr.maxlen);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

可能它不是最好的解决方案,但正如你所说,如果ui-select不支持它,你必须自己做...