已过滤的ng-repeat中的对象数

时间:2015-02-24 21:58:40

标签: javascript angularjs angularjs-ng-repeat ng-repeat angularjs-ng-model

我在网站中使用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 个答案:

答案 0 :(得分:2)

编辑:

根据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

答案 1 :(得分: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

答案 2 :(得分: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>

答案 3 :(得分:0)

我确信这些属性有可能:

  • $最后
  • $索引

类似的东西:

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

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