首页 > 解决方案 > 如何过滤下拉列表并在角度 js 的选择下拉列表中绑定第一个匹配项

问题描述

我有输入文本框来从下拉选择列表中过滤项目。这工作正常。

但是有两个问题。

  1. 当数据被填充时,我希望第一项在下拉列表中可见。
  2. 当我在过滤器框中键入时,列表中的第一个匹配应该显示在下拉控件中。

我如何实现这一目标?

<input type="text" id="fromSearchBox" class="form-control w-auto h-25"
       ng-model="SearchEmployee" />
<select class="form-control" ng-model="fromManagerInfo">
    <option ng-repeat="emp in employees|filter:SearchEmployee">
       {{emp.EmployeeInfo}}
    </option>
</select>

标签: angularjsangularjs-select

解决方案


您可以在 ng-init 上使用 ng-model 设置 selectedItem

<input type="text" ng-init="SearchEmployee = employees[0]" id="fromSearchBox" class="form-control w-auto h-25" ng-model="SearchEmployee" />

对于过滤器,您已经在使用filter


推荐阅读