AngularJS选择 - 匹配第一个字符

ili*_*ica 2 search select angularjs

https://github.com/angular-ui/ui-select

此搜索插件使用找到匹配项'LIKE %word%'.可以将其更改为'LIKE word%' 示例:如果我在搜索状态时键入字母'T',则当前返回以下内容:
H I
I D
I L
I N
I A
M I
R I
...

我想它返回:
ð
大号
ñ
一个

Raj*_*ria 5

您可以使用angular的自定义过滤器来实现此功能.就像是:

app.filter('propsFilter', function() {
  return function(items, props) {
    var out = [];
    if (angular.isArray(items)) {
      items.forEach(function(item) {
        var keys = Object.keys(props);
        var prop = keys[0];
        var text = props[prop].toLowerCase();
        if (item[prop].toString().toLowerCase().indexOf(text) === 0) {
          out.push(item);
        }
      });
    } else {
      out = items;
    }
    return out;
  };
});
Run Code Online (Sandbox Code Playgroud)

并将此过滤器传递给select2声明:

<ui-select ng-model="person.selected" theme="select2" class="form-control" title="Choose a person">
            <ui-select-match placeholder="Select or search a person in the list...">{{$select.selected.name}}</ui-select-match>
            <ui-select-choices repeat="item in people | propsFilter: {name:$select.search}">
              <div ng-bind-html="item.name | highlight: $select.search"></div>
              <small ng-bind-html="item.email | highlight: $select.search"></small>
            </ui-select-choices>
          </ui-select>
Run Code Online (Sandbox Code Playgroud)

以下是您可以参考的plunkr链接: