{"id":1123,"date":"2016-02-22T04:27:11","date_gmt":"2016-02-21T22:57:11","guid":{"rendered":"http:\/\/code.ciphertrick.com\/?p=1123"},"modified":"2024-10-03T11:21:46","modified_gmt":"2024-10-03T11:21:46","slug":"infinite-scroll-angularjs","status":"publish","type":"post","link":"https:\/\/thrivemyway.com\/infinite-scroll-angularjs\/","title":{"rendered":"Developing infinite scroll system using AngularJS"},"content":{"rendered":"\n<p>Infinite scroll in AngularJS is one of the complicated tasks. Infinite scroll not only improves the user interaction but also helps in putting less latency to Server because you don&#8217;t have to load all of the content from the back-end in the browser at once.<\/p>\n\n\n\n<p>In this tutorial, we are addressing the same issue and we will develop a simple application that will have infinite scrolls using the <strong>ngInfiniteScroll<\/strong> directive.<\/p>\n\n\n\n<p class=\"has-text-align-center\">DEMO&nbsp;&nbsp;<a class=\"fasc-button fasc-size-large fasc-type-popout fasc-ico-before dashicons-download\" href=\"https:\/\/github.com\/Inaamhusain\/Developing-infinite-scroll-system-using-AngularJS\" target=\"_blank\" rel=\"noopener\" data-fasc-style=\"background-color:#1fa67a;color:#ffffff;\">DOWNLOAD<\/a><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/thrivemyway.com\/infinite-scroll-angularjs\/#How_ngInfiniteScroll_work\" >How ngInfiniteScroll work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/thrivemyway.com\/infinite-scroll-angularjs\/#How_to_use_ngInfiniteScroll\" >How to use ngInfiniteScroll?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/thrivemyway.com\/infinite-scroll-angularjs\/#Getting_Started\" >Getting Started<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/thrivemyway.com\/infinite-scroll-angularjs\/#Parameters\" >Parameters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/thrivemyway.com\/infinite-scroll-angularjs\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_ngInfiniteScroll_work\"><\/span>How ngInfiniteScroll work?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><kbd>ngInfiniteScroll<\/kbd> is an AngularJS directive, with the help of that we can implement Infinite Scroll in the AngularJS application.<br>So, simply have to make one function that contains our logic to load the next chunk of data. and that will call once you get close to the bottom of the content. and the rest will handle by <kbd>module<\/kbd> and <kbd>directive<\/kbd>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_use_ngInfiniteScroll\"><\/span>How to use ngInfiniteScroll?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Following steps to implement infinite scroll,<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>I will download and add ngInfiniteScroll js file to our application. before that, we also need to add jQuery &amp; AngularJS files to include before them. like below <\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">&lt;script type='text\/javascript' src='path\/to\/jquery.min.js'>&lt;\/script> : jQuery library\n&lt;script type='text\/javascript' src='path\/to\/angular.min.js'>&lt;\/script> : AngularJS library\n&lt;script type='text\/javascript' src='path\/to\/ng-infinite-scroll.min.js'>&lt;\/script> : InfinteScroll directive<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Inject dependency into your main app module. As below shown, <\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">angular.module('testApp', ['infinite-scroll']);<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Specify directive infinite-scroll attribute to an element. <\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">&lt;div infinite-scroll=\"paginationFunction()\">&lt;\/div><\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Now, paginationFunction() function will automatically call once you reach to the bottom of the browser window.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Getting_Started\"><\/span>Getting Started<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>So, lets start with real example to understand how it works. here main index file which contains our specified directive,<\/p>\n\n\n\n<div class=\"head-align\">index.html<\/div>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">&lt;div role=\"main\" class=\"container theme-showcase\" ng-controller=\"scrolling\">\n    &lt;div class=\"main-wrapper\">\n        &lt;div class=\"row\" infinite-scroll='loadMore()'>\n            &lt;div class=\"col-xs-6 col-md-3\" ng-repeat='image in images'>\n                &lt;a class=\"thumbnail\">\n                    &lt;img src=\"http:\/\/placehold.it\/350x250&amp;text={{image}}\" alt=\"{{image}}\">\n                &lt;\/a>\n            &lt;\/div>\n        &lt;\/div>\n    &lt;\/div>\n&lt;\/div><\/code><\/pre>\n\n\n\n<p>in the above code you can see that we have defined directive infinite-scroll with the loadMore function in it. that function will call every time when you reach the bottom.<br>And here is the controller code. In which maintaining an array of data.<\/p>\n\n\n\n<div class=\"head-align\">app\/app.js<\/div>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">var app = angular.module('infiniteScroll', ['infinite-scroll']);\nangular.module('infinite-scroll').value('THROTTLE_MILLISECONDS', 250);\napp.controller('scrolling',function($scope, $http){\n    $scope.images = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];\n    $scope.loadMore = function() {\n        var last = $scope.images[$scope.images.length - 1];\n        for(var i = 1; i &amp;lt;= 12; i++) {\n            $scope.images.push(last + i);\n        }\n    };\n});<\/code><\/pre>\n\n\n\n<p>Here you can see adding throttle for preventing shabby effects on the screen while loading more data. and adding 12 more data on every time when we called that function and maintained <kbd>$scope.images<\/kbd> array.<\/p>\n\n\n\n<p>Lets see what parameters we can pass to the element.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Parameters\"><\/span>Parameters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><kbd>infinite-scroll - {expression}<\/kbd> : Expression is the function that calls every time when element reaches to the bottom of the window.<\/li><li>infinite-scroll-distance (optional) &#8211; {number} : Here number represent the how close the element to the browser window. like if we set it to 1. it means that will call infinite scroll when the bottom is above 1000px.<\/li><li>infinite-scroll-disabled (optional) &#8211; {boolean} : A boolean expression in which we have to pass true or false to stop calling infinite scroll.<\/li><li>infinite-scroll-immediate-check (optional) &#8211; {boolean}: A boolean expression that indicates that the directive should check immediately to see if the scroll event occurred or not. this is useful when the content inside directive element is not tall enough to fill up the entire window.<\/li><li><kbd>infinite-scroll-listen-for-event (optional) - {string}<\/kbd> : A string is name of event that you to trigger manually.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It&#8217;s very easy to implement infinite scroll in the AngularJS application. and that will also improve your performance of the app by loading data on UI in chunks.<br>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Infinite scroll in AngularJS is one of the complicated tasks. Infinite scroll not only improves the user interaction but also [&hellip;]<\/p>\n","protected":false},"author":3000066,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3001981,3001974],"tags":[],"class_list":["post-1123","post","type-post","status-publish","format-standard","hentry","category-angular-js","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/posts\/1123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/users\/3000066"}],"replies":[{"embeddable":true,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/comments?post=1123"}],"version-history":[{"count":1,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/posts\/1123\/revisions"}],"predecessor-version":[{"id":3035566,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/posts\/1123\/revisions\/3035566"}],"wp:attachment":[{"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/media?parent=1123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/categories?post=1123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thrivemyway.com\/wp-json\/wp\/v2\/tags?post=1123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}