角度获取过滤后的 ng-repeat 的长度
Posted
技术标签:
【中文标题】角度获取过滤后的 ng-repeat 的长度【英文标题】:Angular get length of filtered ng-repeat 【发布时间】:2014-02-18 04:58:00 【问题描述】:我已从 v 1.0.8 升级到 Angular 1.2.9,以帮助提高我的应用程序的性能。
我已经介绍了提高速度的轨道。
我曾经拥有的
<div class="result row"
data-ng-repeat="result in (filteredItems =
(results |
filter: filterPrice |
filter: filterCategories |
filter: filterTypes |
filter: filterAirConditioning |
filter: filterTransmission |
filter: filterFourByFour |
filter: filterFuelType |
filter: filterNumberOfDoors |
filter: filterOnAirport |
filter: filterFuelPolicy |
filter: filterUnlimitedMileage |
filter: filterFreeCancellation |
filter: filterTheftProtection |
filter: filterCDW |
filter: filterTaxesIncluded |
filter: filterProviders ) ) |
orderBy: [orderByOptions, orderByPriceLowToHigh, orderByPriority] |
limitTo: pageSize">
我现在拥有的
<div class="result row"
data-ng-repeat="result in results |
filter: filterPrice |
filter: filterCarCategories |
filter: filterCarTypes |
filter: filterAirConditioning |
filter: filterTransmission |
filter: filterFourByFour |
filter: filterFuelType |
filter: filterNumberOfDoors |
filter: filterOnAirport |
filter: filterFuelPolicy |
filter: filterUnlimitedMileage |
filter: filterFreeCancellation |
filter: filterTheftProtection |
filter: filterCDW |
filter: filterTaxesIncluded |
filter: filterProviders |
orderBy: [orderByOptions, orderByPriceLowToHigh, orderByPriority] |
startFrom: currentPage*pageSize |
limitTo: pageSize track by $index">
我有寻找已过滤项目长度的分页。那已经不存在了。此外,其他一些 JS 需要过滤项目来进行日志记录、分析等
在 html 视图中,我可以在应用过滤器后获取 ng-repeat 的长度
(results |
filter: filterPrice |
filter: filterCarCategories |
filter: filterCarTypes |
filter: filterAirConditioning |
filter: filterTransmission |
filter: filterFourByFour |
filter: filterFuelType |
filter: filterNumberOfDoors |
filter: filterOnAirport |
filter: filterFuelPolicy |
filter: filterUnlimitedMileage |
filter: filterFreeCancellation |
filter: filterTheftProtection |
filter: filterCDW |
filter: filterTaxesIncluded |
filter: filterProviders)
.length
这有点拗口,但确实返回了我想要的数字。如何将其传递给 $scope ?
【问题讨论】:
【参考方案1】:您可以创建一个中间变量来保存过滤后的数组:
data-ng-repeat="result in filteredResults = (results | filter:filterPrice | filter:filterCarCategories | etc.)"
然后可以使用:
filteredResults.length
【讨论】:
嗨,Gruff Bunny,我确实有类似的东西,请参阅代码的第一部分。我刚刚尝试了您的版本,但收到此错误:错误:[ngRepeat:dupes] 我以为我必须设置一个 $scope.filteredResults,所以就这样做了。 你有没有在表达式末尾通过 $index 进行跟踪(以应对重复)? 在这里得到它: 现在不会抛出错误,但过滤结果.length 返回总结果数组,并且在应用过滤器后不会更改。我会敲一个演示...... 实际上,如果我将括号移到不同的位置,它会返回正确的数量以上是关于角度获取过滤后的 ng-repeat 的长度的主要内容,如果未能解决你的问题,请参考以下文章