【问题标题】:How to set the throttle parameter in ngInfiniteScroll (AngularJS)如何在 ngInfiniteScroll (AngularJS) 中设置油门参数
【发布时间】:2017-09-01 14:38:02
【问题描述】:

在我的 Angular 应用程序中,我使用 ng-infinite-scroll 来允许用户使用此处的插件连续滚动浏览他们的“新闻提要” - https://sroze.github.io/ngInfiniteScroll/documentation.html

在我的桌面上它运行良好,但是在 Android 设备上的 Cordova 应用程序中运行它时,无限滚动会占用大量 CPU 资源。我正在尝试使用 THROTTLE_MILLISECONDS 选项仅每 x 秒处理一次滚动事件(这应该会提高性能并使滚动不那么生涩)。

我的模块定义如下:

var abcdDirectives = angular.module('abcdDirectives', []);
var abcdServices = angular.module('abcdServices', [ 'ngResource', 'infinite-scroll', 'ngCookies']);

abcdApp.value('THROTTLE_MILLISECONDS', 10000);

我正在尝试使用上面的行按如下方式限制它,但它似乎没有任何区别。

在我的模板视图中,我有以下代码:

<div 
  infinite-scroll="tabs[tabIndex].FeedService.loadFeed(false, tabs[tabIndex].FeedService.filters, search.text, search.dateFrom, search.dateTo)"
  infinite-scroll-disabled="tabs[tabIndex].FeedService.busy || tabs[tabIndex].FeedService.noMoreResults || !tabs[tabIndex].active || tabs[tabIndex].FeedService.initialLoad"
  infinite-scroll-distance="1"
  infinite-scroll-immediate-check="false" >
<div ng-repeat="interaction in getTabItems(tabIndex) track by $index">

在 js 控制器中是我的 getTabItems 函数

$scope.getTabItems = function (index) {
    if (angular.isDefined($scope.tabs[index].FeedService)) {
        console.log('getTabItems'); // this is being output to the console log over and over again extremely quickly
        return $scope.tabs[index].FeedService.items;
    }
}

上面函数中的控制台日志我可以在控制台日志中看到一遍又一遍地输出太多&我试图限制这个函数被调用但是我有限制语句用过好像没什么区别?我对代码做错了什么

-- 版本--

Angular 1.3.0 ng-infinite-scroll 1.2.2

【问题讨论】:

    标签: javascript angularjs cordova infinite-scroll nginfinitescroll


    【解决方案1】:

    THROTTLE_MILLISECONDS 应该设置在将使用ngInfiniteScroll 的模块上。所以对于你的情况,它应该设置在abcdServices,像这样。

    var abcdDirectives = angular.module('abcdDirectives', []);
    var abcdServices = angular.module('abcdServices', [ 'ngResource', 'infinite-scroll', 'ngCookies']);
    
    abcdServices.value('THROTTLE_MILLISECONDS', 10000);
    

    但在我看来,价值应该在于它的直接父母(使用ngInfiniteScroll)。像这样。

    angular.module('yourApp.controllers', [])
    .value('THROTTLE_MILLISECONDS', 5000)
    .controller('controllerWhichUseInfiniteScroll',
    ['$scope',
        function ($scope) {
        }
    ]);
    

    如果在渲染tabs[tabIndex].FeedService.loadFeed 的新结果之前将infinite-scroll-disabled 标志设置为true,则infinite-scroll 事件函数(在您的情况下为tabs[tabIndex].FeedService.loadFeed)将无限循环运行。

    要解决此问题,请在下一个摘要循环中使用 $timeoutinfinite-scroll-disabled 标志设置为 true。这意味着标志只有在渲染结果完成时才会为真。见下文...

    <div 
      infinite-scroll="getDataFromApi()"
      infinite-scroll-disabled="loaded"
      infinite-scroll-distance="1">
        <div ng-repeat="data in dataList">
    

    --

    angular.module('yourApp.controllers')
    .controller('yourController',
    ['$scope', '$timeout', 'apiDataService',
        function ($scope,  $timeout, apiDataService) {
            $scope.dataList = [];
    
            $scope.getDataFromApi = function () {
                $scope.loaded = false;
                apiDataService.getData()
                    .then(function(result) {
                        $scope.dataList = result.data;
    
                        //Set loaded to true after rendering new results is finished.Otherwise it will make infinite api calls.
                        $timeout(function (){
                            $scope.loaded = true;
                        });
                    });
            }
        }
    ]);
    

    还值得指出的是,出于性能原因,getTabItems() 不应用于绑定 html 中的数据。因为 Angular 会将其置于摘要循环中以进行更改检测,并且无论您是否使用 ngInfiniteScroll 都会被大量调用。

    【讨论】:

      猜你喜欢
      • 2018-02-25
      • 1970-01-01
      • 2019-11-25
      • 2021-08-23
      • 2021-12-13
      • 1970-01-01
      • 2012-12-22
      • 2018-06-02
      • 2018-12-15
      相关资源
      最近更新 更多