大佬教程收集整理的这篇文章主要介绍了AngularJS’ng-filter’在〜1000个元素的数组上非常慢,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的列表看起来像这样:
var uniqueLists = { category1: ['item1','item2','item3' ... 'item180' ],// Real list contains ~180 items category2: ['itemA','itemB','itemC' ... 'itemZZZ' ],// Real list contains ~1080 items category3: ['otheritem1','otheritem2','otheritem3' ] // Real list contains 6 items }
我在Angular中遍历该列表,并将结果打印在< ul>为每个类别。
<div ng-repeat="(key,val) in uniqueLists"> <form ng-model="uniqueLists[index][0]"> <input ng-model="searchFilter" type="text" /> <ul> <li ng-repeat="value in val | filter: searchFilter"> <label> <input type="checkBox" ng-model="SELEctedData[key][value]" /> {{value}} </label> </li> </ul> </form> </div>
var SELEctedData = {Category1: [item1:true],category2: [],category3: []); // if 'item1's checkBox is checked.
这个列表工作正常,虽然过滤器是相当滞后,即使在我非常快的电脑。在输入中键入一个字母需要1-2秒的时间来更新列表。
我知道这可能是因为我一次过滤大约1000项,但我还没有看到任何讨论这一点。
有什么办法让过滤器更好的性能吗?
ng-show="([item] | filter:searchFilter).length > 0"
对重复的元素。
从@OverZealous中借出一些代码,您可以使用以下来比较行为:
> filter:http://jsbin.com/fuwadanu/1/
> ng-show:http://jsbin.com/xajehulo/1/
更新:使用Angular v1.2通过语法来跟踪。这也有助于这样的问题。如果元素有一些唯一的属性,可以使用:
ng-repeat="item in items | filter:searchFilter track by item.id"
其中item.id必须在所有项目之间是唯一的。跟踪只有那些dom元素将被删除,这不再是最终列表中,其他人将被记住。而无轨道的整个列表是每次重绘。简而言之:更少的dom操作=更快的重画。
> track by:http://jsbin.com/dufasego/1/
以上是大佬教程为你收集整理的AngularJS’ng-filter’在〜1000个元素的数组上非常慢全部内容,希望文章能够帮你解决AngularJS’ng-filter’在〜1000个元素的数组上非常慢所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。