过滤的 ng-repeat 中的对象数

Number of objects in a ng-repeat that is filtered

我在一个站点中使用 angularjs,我有一个搜索输入可以过滤视图中的列表。此列表与 ng-repeat 一起显示,该 ng-repeat 具有来自搜索输入的过滤器:

搜索输入:

<input type="text" placeholder="Device Search" class="form-control hasclear" 
  ng-model="searchText"/>

这里是 ng-repeat:

<tr ng-click="openModal(device['device_id'], device)" 
  ng-repeat="device in devices | filter:searchText | orderBy:predicate:reverse">

如您所见,ng-repeat 中的过滤器具有使用 ng-model 中的 searchText 变量的过滤器。我想知道是否有可能知道当用户在搜索输入中输入文本时找到了多少个对象(ng-repeat 显示有多少设备被过滤)。喜欢:0 devices were found3 devices were found...

有没有办法按照我构建此搜索的方式显示此信息?

我确信属性有可能:

  • $最后
  • $索引

类似于:

<span ng-if="$last" > {{$index}} devices found </span>

文档:https://docs.angularjs.org/api/ng/directive/ngRepeat

编辑:

根据 Dave 的正确回答,您必须为过滤后的结果分配一个变量

<tr ng-click="openModal(device['device_id'], device)" 
ng-repeat="device in filtered_devices = (devices | filter:searchText | orderBy:predicate:reverse)">

然后,您可以在 tr 标签下方键入:

<div>{{filtered_devices.length + ' devices were found'}}</div>

Plunkr 示例 here

如果您在 <tr> 标签之外需要它,我会这样做:

<tr ng-click="openModal(device['device_id'], device)" 
ng-repeat="device in filteredResults = (devices | filter:searchText | orderBy:predicate:reverse)">

{{filteredResults.length}} devices were found

使用像 filter:x as result 这样的过滤器,它将结果集合存储在变量 result 上。

所以你可以这样做:

<tr ng-click="openModal(device['device_id'], device)" ng-repeat="device in devices | filter:searchText as filteredCollection | orderBy:predicate:reverse">
  <td><span>Filtered collection size: {{filteredCollection.length}}</span><td>
</tr>