AngularJS - 一个简单的无限滚动

时间:2014-07-08 09:12:11

标签: angularjs angularjs-directive angularjs-ng-repeat infinite-scroll

我正在尝试写一个类似的小无限滚动到这里找到的滚动:http://jsfiddle.net/vojtajina/U7Bz9/

我已经能够显示前5个数据,但是在滚动时,其他项目没有显示。

HTML:

<div id="fixed" directive-when-scrolled="loadMore()">
  <ul>
    <li ng-repeat="i in items | limitTo: 5">{{ i.Title }}</li>
  </ul>  
</div>

JS:

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.items = [
    {
       "Title":"Home"
    },
    {
       "Title":"Contact"
    },
    {
       "Title":"Help"
    },
    {
       "Title":"About"
    },
    {
       "Title":"Our Offices"
    },
    {
       "Title":"Our Locations"
    },
    {
       "Title":"Meet the Team"
    }
    ,
    {
       "Title":"Our Mission"
    },
    {
       "Title":"Join Us"
    },
    {
       "Title":"Conferences"
    },
    {
       "Title":"Tables"
    },
    {
       "Title":"Chairs"
    } 
  ];

    var counter = 0;
    $scope.loadMore = function() {
        for (var i = 0; i < 5; i++) {
            $scope.items.push({id: counter});
            counter += 10;
        }
    };

    $scope.loadMore();


});


app.directive("directiveWhenScrolled", function () {
  return function(scope, elm, attr) {
        var raw = elm[0];

        elm.bind('app', function() {
            if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) {
                scope.$apply(attr.whenScrolled);
            }
        });
    };
});

这里是一名掠夺者:http://plnkr.co/edit/6ggYGZx6scSoJ5PNhj67?p=preview

2 个答案:

答案 0 :(得分:8)

有两个问题。首先,attr.whenScrolled是什么?它未定义。第二个 - limitTo: 5。你总是只展示5个元素!

这里有工作代码:http://plnkr.co/edit/VszvMnWCGb662azo4wAv?p=preview

改变了什么?您的指令名为directiveWhenScrolled,因此请致电:

scope.$apply(attr.directiveWhenScrolled);

而不是

scope.$apply(attr.whenScrolled);

如何处理静态限制。将其更改为变量(记住定义默认值):

<li ng-repeat="i in items | limitTo: limit">{{ i.Title }}</li>

现在您的loadMore函数应如下所示:

$scope.loadMore = function() {
    $scope.limit += 5;
};

答案 1 :(得分:0)

我发现了一个很棒的angularjs 'infinite' scroll solution

您需要做的就是在项目中添加一个in-view directive并做下一件事情:

angular.module('infinitScrollApp', ['angular-inview'])
       .controller('infinitScrollController', infinitScrollController);
       
function infinitScrollController($scope) {
  $scope.limit = 10;
  $scope.items = Array.from(Array(1000).keys());
  
  $scope.loadMore = function (last, inview) {
      if (last && inview) {
          $scope.limit += 10;
      }
  }
}
.infinit-scroll-container {
  height: 150px;
  overflow-y:scroll
}
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js">
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-inview/3.0.0/angular-inview.min.js">
    </script>
  </head>

  <body ng-app="infinitScrollApp">
    <div class="infinit-scroll-container">
        <ul ng-controller="infinitScrollController">
            <li ng-repeat="item in items | limitTo: limit" in-view="loadMore($last, $inview)">
            {{item}}
            </li>
        </ul>
    </div>
  </body>
</html>