1

我在一个站点中使用 angularjs,并且我有一个搜索输入来过滤视图上的列表。此列表显示为带有来自搜索输入的过滤器的 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...

有没有办法通过我建立这个搜索的方式来显示这些信息?

4

4 回答 4

2

编辑:

根据戴夫的正确响应,您必须为过滤结果分配一个变量,如下所示

<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 示例在这里

于 2015-02-24T22:08:34.600 回答
2

如果您在<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
于 2015-02-24T22:10:16.697 回答
1

使用过滤器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>
于 2015-02-24T22:10:23.960 回答
0

我确信这些属性有可能:

  • $最后
  • $索引

就像是:

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

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

于 2015-02-24T22:08:04.597 回答