简体   繁体   English

角度大的json文件

[英]Angular large json file

I've read that best practice when scaling Angular is to limit the viewable area in an ng-repeat. 我已经阅读了扩展Angular时的最佳实践,即限制ng-repeat中的可视区域。 I've done that (I believe) in this case by limitTo (and, this is a true limitTo...no infinite scrolling or anything yet): 在这种情况下我通过limitTo完成了(我相信)(并且,这是一个真正的限制...没有无限滚动或任何东西):

<li ng-repeat="lister in videos | filter:query | orderBy:orderProp | limitTo:20"
                 class="video_link">
   <p class="background_435F2C">
        <a id="{{lister.id}}" href="#/videos/{{lister.id}}">{{lister.name}}</a>
   </p>
</li>

However, when I bring in test data via a JSON file with 20,000 simple records, response time on the associated query filter (just the standard Angular query, nothing fancy) declines. 但是,当我通过带有20,000条简单记录的JSON文件引入测试数据时,相关查询过滤器的响应时间(仅标准的Angular查询,没有任何花哨)会下降。 There's noticeable lag when typing in the input. 输入输入时有明显的延迟。

Any thoughts? 有什么想法吗? Is filtering a large JSON file like this best practice, or is it really something that should be done server-side, going to a /search page? 过滤像这样的最佳实践的大型JSON文件,还是应该在服务器端完成,转到/搜索页面?

Thanks! 谢谢!

In cases like yours, do the filtering in JS code in controller, not in the HTML (because it eventually runs twice if in HTML). 在像你这样的情况下,在控制器中的JS代码中进行过滤,而不是在HTML中进行过滤(因为如果在HTML中最终运行两次)。

// inject $filter into controller
.controller('MyCtrl', ['$scope', '$filter', function ($scope,$filter) {
...

var filterFilter = $filter('filter');
var orderByFilter = $filter('orderBy');
var limitToFilter = $filter('limitTo');

var tmp = filterFilter($scope.videos , $scope.query);
tmp = orderByFilter(tmp, $scope.orderProp);
$scope.videosPreFiltered = limitToFilter(tmp, 20);

Then in html 然后在HTML中

<li ng-repeat="lister in videosPreFiltered class="video_link">
   <p class="background_435F2C">
        <a id="{{lister.id}}" href="#/videos/{{lister.id}}">{{lister.name}}</a>
   </p>
</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM