如何在ngInfiniteScroll中设置油门参数(AngularJS)
How to set the throttle parameter in ngInfiniteScroll (AngularJS)
在我的 Angular 应用程序中,我使用 ng-infinite-scroll 允许用户使用此处的插件连续滚动浏览他们的 'news feed' -
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;
}
}
我在控制台日志中看到的上面函数中的控制台日志正在一遍又一遍地输出太多了&我试图限制这个函数被调用但是我用过的throttle语句好像没什么区别?我对代码做错了什么
-- 版本 --
Angular 1.3.0
ng-infinite-scroll 1.2.2
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) {
}
]);
infinite-scroll
事件函数(tabs[tabIndex].FeedService.loadFeed
在你的例子中)如果你在呈现 tabs[tabIndex].FeedService.loadFeed
完成。
要解决此问题,请使用 $timeout
在下一个摘要循环中将 infinite-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
都会被调用很多次。
在我的 Angular 应用程序中,我使用 ng-infinite-scroll 允许用户使用此处的插件连续滚动浏览他们的 'news feed' - 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;
}
}
我在控制台日志中看到的上面函数中的控制台日志正在一遍又一遍地输出太多了&我试图限制这个函数被调用但是我用过的throttle语句好像没什么区别?我对代码做错了什么
-- 版本 --
Angular 1.3.0
ng-infinite-scroll 1.2.2
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) {
}
]);
infinite-scroll
事件函数(tabs[tabIndex].FeedService.loadFeed
在你的例子中)如果你在呈现 tabs[tabIndex].FeedService.loadFeed
完成。
要解决此问题,请使用 $timeout
在下一个摘要循环中将 infinite-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
都会被调用很多次。