Infinite scroll in Angularjs with external JSON

Asked

Viewed 1,428 times

6

I’m having a bit of trouble creating an infinite scroll in Angularjs from a JSON file generated on an external site. What I need is for the infinite scroll to be called when the variable item posts is equal to 10, calling again my function, incrementing +1 in the URL, changing the page. Follow the code:

$scope.posts = [];
    $scope.doRefresh = function(){
        JsonNews.getBlogs($scope);
        $scope.$broadcast('scroll.refreshComplete');
    }
    $page = 0;
    function JsonNews($http, $log){
        $page++;
        $urlInfinite = "http://plantaojti.com.br/noticias/page/"+ $page +"/?feed=json&callback=JSON_CALLBACK";
        this.getBlogs = function($scope){
            $http.jsonp($urlInfinite).success(function(data){
                $scope.posts = data;
                for (var i = 0; i < $scope.posts.length; i++) {
                    this.posts.push(posts[i].data);
                }
                AppCtrl.$scope.doRefresh($scope);
            });
        }
    }

In HTML:

<div infinite-scroll='JsonNews()' inite-scroll-distance='1'>
        <ion-item data-ng-repeat="item in posts | filter: query" class="item-thumbnail-left item-text-wrap item-icon-right" href="window.open('{{item.permalink}}', '_system', 'location=yes'">
                    <img class="thumb-noticia" data-ng-src="{{item.thumbnail}}">
                    <h2> {{item.title}} </h2>
                    <p>{{item.excerpt | limitTo: 100}}...</p>
         </ion-item>
</div>
  • I have long used a plugin bait to browse a list in javascript. It wasn’t an infinite scroll, but I’ve been watching and they even have a demo to an infinite. I don’t know if you can adapt it to angularJS

  • After much trying to do, I discovered the ng-Infinite-scroll, that completely solved my problem.

  • I left a solution that I use right below, I hope you like it.

  • 3

    Possible duplicate of Load list by parts using Angularjs

1 answer

3

I do not use any plugin p/ do angular scroll Infinit, in my case.

Html Example.

<div data-ng-repeat="item in lista" data-infinit-scroll-page="loadInfinit()"> // nessa div eu coloco p/ dar barra de rolagem
   <input type="text" data-ng-model="item.nome">
</div>

Example controller.

controller('textController', ['$scope', '$http' function($scope, $http){

    $scope.lista = []; //Aqui você inicializa sua lista vazia.
    $scope.pag = 0;

    $scope.loadInfinit = function(){

    $http.get('url + $scope.pag').success(function(retorno){

         $scope.pag += 1; //incremento +1 na minha pagina
         angular.forEach(retorno, function(value, index){
              $scope.lista.push(value)
         });    
    });    

   }

   $scope.loadInfinit(); // depois da criação do controller, já mando ler a 1ª vez os registros.

}]);


app.directive('infinitScrollPage', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            var elm = $(document);
            elm.bind('scroll', function () {
                var porcentagem = (($(window).scrollTop() + window.innerHeight) / $(this).height()).toFixed(2);
                if (porcentagem >= 0.95) { //só aplica o scroll se for maior igual a 95%
                    scope.$apply(attr.infinitScrollPage);
                }
            });
        }
    };
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.