【问题标题】:The ng-repeat div disappears after the second page is loaded with ngInfiniteScrollng-repeat div 在第二页加载 ngInfiniteScroll 后消失
【发布时间】: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


【解决方案1】:
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');

        // change this
        // $scope.channel = [];
        $scope.channel = {contents:[]};

        // remove this, double request
        // 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.imageloaded = function() {
          $scope.imagesloaded++;
          if ($scope.imagesloaded >= $scope.channel.contents.length)
            $scope.busy = false;
        }
        $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() {
                    // change this
                    // $scope.channel = channel;
                    $scope.channel.contents = $scope.channel.contents.concat(channel.contents);
                    $scope.imagesloaded = 0;
            });
        }
    });

// add a directive in javascript
app.directive("imageLoaded",  function() { 
    return {
      restrict: "A",
      link: function(scope, elem, attr) {
        elem.on('load', function() {
          scope.$parent.imageloaded();

        });
      }
    };
  });


<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">
                <!-- use ng-src for image read: http://docs.angularjs.org/api/ng/directive/ngSrc  -->
                <img ng-src="{{block.image.original.url}}" image-loaded alt="{{block.title}}">
            </div> 
        </div>
    </div>
    <div ng-show="busy">Loading</div>
</div>

【讨论】:

  • 谢谢。它现在正确加载第二个 appendContents() ,但不是将它们附加到现有内容下方,而是将第一页完全替换为第二页。见nolayout.com/archive/index-3.html
  • 在 $scope.channel 中收到结果后使用 Array.concat
  • 我试过了,没成功,如果你有耐心告诉我我会非常感激(我很新手,学习很辛苦)。
  • 最后一个问题,可以忽略。 concat 工作,但我仍然有容器的问题:它正确加载图像的第二页,但不是第三页,依此类推,因为它没有检测到容器的结尾......
  • 您确定没有启动无限滚动检测吗?我在想指令 imageLoaded。如果您的一张图片未完全加载/错误,则 $scope.busy 将再次永远为假。我不能测试,所以我不能告诉你。您需要逐步完成该部分。可能会更改($scope.imagesloaded >= $scope.channel.contents.length),如果加载了一半的图像,则将其设置为 true。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多