当用户向下滚动浏览器窗口时,我必须显示更多图像。我有 为此创建了一个指令。但滚动事件不会被触发。
app.directive('whenScrolled', function() {
return function(scope, elm, attr) {
var raw = elm[0];
elm.bind('scroll', function() {
if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) {
scope.$apply(attr.whenScrolled);
}
});
};
});
在LoadMoreItems中,我将两个新项目推送到列表中:
function LoadMoreItems() {
$scope.temp = $scope.allItems[$scope.lastCount];
$scope.items.push({
Name: $scope.temp.Name,
Price: $scope.temp.Price,
StockStatus: $scope.temp.StockStatus,
Picture: $scope.temp.Picture
});
$scope.temp = $scope.allItems[$scope.lastCount + 1];
$scope.items.push({
Name: $scope.temp.Name,
Price: $scope.temp.Price,
StockStatus: $scope.temp.StockStatus,
Picture: $scope.temp.Picture
});
$scope.lastCount += $scope.count;
}
以下功能会显示该类别的所有特定项目。
function getAllItemss(cat) {
itemsFactory.getSpecificItems(cat)
.success(function(_items) {
$scope.allItems = _items;
//$scope.items = [];
$scope.lastCount = 0;
LoadMoreItems();
})
.error(function(error) {
$scope.status = 'Unable to load items : ' + error.message;
});
}
HTML:
<div when-Scrolled="LoadMoreItems()"></div>