0

Below is a snippet using AngularJS to get the JSON response.

On scrolling to the bottom of the page, I need to request repeatedly till the JSON array list is empty.

<section>
        <div style="padding-top:60px" infinite-scroll="myPagingFunction()" infinite-scroll-distance="3" ng-app="myApp" ng-controller="myCtrl"> 
            <div ng-repeat="news in newsList">

                <div class="col-lg-3 col-md-4 col-sm-6" style="">
                    <div class="thumbnail">
                        <img src="{{news.coverUrl}}" class="img-responsive"  alt="{{news.name}}"/>
                        <div class="caption">
                            <h4>{{news.name}}</h4>
                        </div>
                    </div>
                </div>
                <div class="visible-lg clearfix" ng-if="($index+1)%4 == 0"></div>
                <div class="visible-md clearfix" ng-if="($index+1)%3 == 0"></div>
                <div class="visible-sm clearfix" ng-if="($index+1)%2 == 0"></div>

            </div>
        </div>
    </section>
    <script src="js/jquery.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="js/ng-infinite-scroll.min.js"></script>


    <script>
        var offset = 0 
        var maxCount = 20
        var app = angular.module('myApp', ['infinite-scroll']);
        function myPagingFunction(){

            app.controller('myCtrl', function($scope, $http) {
                $http.get("news.json").then(function(response) {
                  $scope.newsList = response.data; 
                });
            });
        }
        myPagingFunction();
    </script>

    <script src="js/bootstrap.min.js"></script>

Kindly help me in solving this.

2
  • 1
    Using jquery with angular is a big no.Though in theory it can be done by writing jquery code in apply method,jquery shouldnt be used.Use only javascript.Otherwise , javascript DOM manipulation will not be caught by angular. Commented Apr 16, 2016 at 9:24
  • Anyway below answer is one of the best way to implement the functionality Commented Apr 16, 2016 at 9:25

3 Answers 3

1

Using jQuery + angular is not good
You must use angular way instead, and write some directives or listeners in run phase, but here's a better approach - use `ngInfiniteScroll plugin

Sign up to request clarification or add additional context in comments.

1 Comment

I have use nginfinitescroll, but didn't work. Can you please alter my code to make it functional
0

You can simply use infinite Scroll services to easily achieve this ,

    <ANY infinite-scroll='{expression}'
 [infinite-scroll-distance='{number}']
 [infinite-scroll-disabled='{boolean}']
 [infinite-scroll-immediate-check='{boolean}']
 [infinite-scroll-listen-for-event='{string}']>

Add ng-infinite scroll module in your application for more info refer this [Documentation]:(https://sroze.github.io/ngInfiniteScroll)

4 Comments

in your html page add <div infinite-scroll="myPagingFunction()" infinite-scroll-distance="3"></div> and define your myPagingFunction() inside Controller ..
app.controller('myCtrl', function($scope, $http) { $scope.myPagingFunction = function(){ $http.get("news.json").then(function(response) { $scope.newsList = response.data; }); } }); change your code like this , and check it works , if you need further help , create a plunkr
how to append new data inside $scope.newsList
use array push method, ie $scope.newsList.push(response); or use concatenate
0

You can use this simple directive and enjoy:

app.directive("scroll",['$window', function ($window) {
return function (scope, element, attrs) {
    angular.element($window).bind("scroll", function () {

        if (document.body.scrollHeight - $window.scrollY < 890) {
            scope.$apply(function () {

                scope.myPagingFunction();
            });
        }

    });
};
 }]);

and you can use this code for your situation

    <section>
        <div style="padding-top:60px" scroll ng-app="myApp" ng-controller="myCtrl"> 
            <div ng-repeat="news in newsList">

                <div class="col-lg-3 col-md-4 col-sm-6" style="">
                    <div class="thumbnail">
                        <img src="{{news.coverUrl}}" class="img-responsive"  alt="{{news.name}}"/>
                        <div class="caption">
                            <h4>{{news.name}}</h4>
                        </div>
                    </div>
                </div>
                <div class="visible-lg clearfix" ng-if="($index+1)%4 == 0"></div>
                <div class="visible-md clearfix" ng-if="($index+1)%3 == 0"></div>
                <div class="visible-sm clearfix" ng-if="($index+1)%2 == 0"></div>

            </div>
        </div>
    </section>
    <script src="js/jquery.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="js/ng-infinite-scroll.min.js"></script>


    <script>
        var offset = 0 
        var maxCount = 20
        var app = angular.module('myApp', ['infinite-scroll']);
         app.controller('myCtrl', function ($scope, $http) {
    $scope.myPagingFunction= function() {
        $http.get("news.json").then(function (response) {
            $scope.newsList = response.data;
        });
    }
             });
    app.directive("scroll",['$window', function ($window) {
  return function (scope, element, attrs) {
angular.element($window).bind("scroll", function () {

    if (document.body.scrollHeight - $window.scrollY < 890) {
        scope.$apply(function () {

            scope.myPagingFunction();
            });
        }

    });
};
}]);
    </script>

    <script src="js/bootstrap.min.js"></script>

1 Comment

Can you please change my code to make it functional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.