Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngInfiniteScroll is not working

I am trying to recreate a simple example of ngInfiniteScroll provided on their demo page. The loadMore() function never triggers and I do not know why.

Here's the code: http://jsfiddle.net/uVxb8/3/.

<div ng-app='myApp' ng-controller='DemoController'>
  <div infinite-scroll='loadMore()'  infinite-scroll-distance='2'>
    <img ng-repeat='image in images' ng-src='http://placehold.it/225x250&text={{image}}'>
  </div>
</div>

var myApp = angular.module('myApp', ['infinite-scroll']);

myApp.controller('DemoController', function($scope) {
  $scope.images = [1, 2, 3, 4, 5, 6, 7, 8];

  $scope.loadMore = function() {
    var last = $scope.images[$scope.images.length - 1];
    for(var i = 1; i <= 8; i++) {
      $scope.images.push(last + i);
    }
  };
});
like image 402
George Richardson Avatar asked Dec 05 '25 16:12

George Richardson


1 Answers

Your method looks very complicated. Here is what I wrote (and took from another question here I believe) in the directive, to do I believe the exact same behaviour (when scrolled down, it triggers a function that loads more of the same content):

RandomName.directive('isScrolled', ['$window', '$document', function($window, $document) {
    return {

        link: function($scope, elem, attr) {
                var $myWindow = angular.element($window);
                var $myDoc = angular.element($document);

                $myWindow.bind('scroll', function() {
                    if ($myWindow.height() + $myWindow.scrollTop() >= $myDoc.height()) {
                        $scope.$apply(attr.isScrolled);
                    }
                });
            }
    }
}]);

I "listen" to the scroll event, and then everytime it happens I check if it's at the end of the page, if it is I trigger my function through the attribute. In this specific case, it triggers it only when your scroll to the very end, you probably can do something smoother.

like image 55
Mimu Avatar answered Dec 08 '25 09:12

Mimu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!