【发布时间】:2014-03-05 00:43:40
【问题描述】:
这个 Angular 脚本从 API 获取内容,我实现了 ngInfiniteScroll 以避免一次从源获取整个内容。当 ngInfiniteScroll 函数触发加载第二个页面时,您可以看到内容加载了几分之一秒,但随后整个 div 消失了。知道为什么吗?
现场示例
http://nolayout.com/archive/index-2.html
角脚本
var app = angular.module('arenaApp', ['ngResource', 'ngSanitize', 'infinite-scroll']);
app.controller('channelShow', function($scope, $resource, $routeParams, $rootScope) {
var Channel = $resource('http://api.are.na/v2/channels/:slug/contents');
$scope.channel = [];
var channel = Channel.get({ slug: 'no-layout-archive', per: 20, sort: 'position', direction: 'asc' }, function() {
$scope.channel = channel;
});
$scope.busy = false;
$scope.page = 1;
$scope.appendContents = function() {
if ($scope.busy) return;
$scope.busy = true;
$scope.page++;
var channel = Channel.get({ slug: 'no-layout-archive', per: 20, page: $scope.page, sort: 'position', direction: 'asc' }, function() {
$scope.channel = channel;
$scope.busy = false;
});
}
});
HTML
<div class="container" infinite-scroll="appendContents()" infinite-scroll-disabled="busy" infinite-scroll-distance="1" >
<div ng-repeat="block in channel.contents | filter:query" >
<div ng-if="block.class == 'Image'">
<div class="seven columns add-bottom imgnormal">
<img src="{{block.image.original.url}}" alt="{{block.title}}">
</div>
</div>
</div>
<div ng-show="busy">Loading</div>
</div>
【问题讨论】:
-
您的 $scope.appendContents 被调用了 6 次,并且您的内容在一卷中用尽。你需要保持 $scope.busy = false 直到图像被加载或做一些事情来增加你的容器的高度
标签: javascript angularjs infinite-scroll nginfinitescroll