AngularJS: 获取复数过滤数组的大小

AngularJS: Get size of complex filtered array

我在 ng-repeat 上有一组复杂的过滤器。我正在尝试获取数据集的计数,因为它是通过每个 select 列表过滤的。

这是我的 ng-repeat:

  <div class="trials-item {{class}}" ng-repeat="data in dataObject | byCountry : selectRegion | byRegion : selectState | byCity : selectCity | filterBy:['Phase']: selectPhase | filterBy:['Compound']: selectCompound | filterBy:['TherapeuticArea', 'TherapeuticArea_2',]: selectTherapy : 'strict' | unique: 'Id' | orderBy:'Phase' : reverse track by $index " ng-class="{'open':$index == selectedRow}" id="anchor-{{$index}}" >

尝试使用 {{ data.length }} 无效。我尝试使用 post: How to display length of filtered ng-repeat data 中详述的一些解决方案,但它们都处理一组更简单的过滤器,如果它们适用于我上面的过滤器集,我必须syntax/order 不正确。

您只需要为过滤后的 dataObject 分配一个变量,并确保在 in 关键字之后和 track 关键字之前用括号将赋值表达式括起来单词。

<div class="trials-item {{class}}" ng-repeat="data in (filteredData = (dataObject | byCountry : selectRegion | byRegion : selectState | byCity : selectCity | filterBy:['Phase']: selectPhase | filterBy:['Compound']: selectCompound | filterBy:['TherapeuticArea', 'TherapeuticArea_2',]: selectTherapy : 'strict' | unique: 'Id' | orderBy:'Phase' : reverse)) track by $index " ng-class="{'open':$index == selectedRow}" id="anchor-{{$index}}" ></div>
Result: {{ filteredData.length }}

更新

如果您对变量的范围有疑问,请创建一个对象 $scope 变量来存储过滤后的数据。

Javascript

$scope.filtered = {};

HTML

<div class="trials-item {{class}}" ng-repeat="data in (filtered.data = (dataObject | byCountry : selectRegion | byRegion : selectState | byCity : selectCity | filterBy:['Phase']: selectPhase | filterBy:['Compound']: selectCompound | filterBy:['TherapeuticArea', 'TherapeuticArea_2',]: selectTherapy : 'strict' | unique: 'Id' | orderBy:'Phase' : reverse)) track by $index " ng-class="{'open':$index == selectedRow}" id="anchor-{{$index}}" ></div>
Result: {{ filtered.data.length }}