Today we are going to implement infinite/unlimited scrolling using AngularJS in MVC application.
Infinite/unlimited scroll worked while we scroll down the web page it’ll automatically load next paged data. Facebook has this type of News feed loading.
Let’s Get Started:
Let’s create a view with directive ‘when-scrolled’. AngularJS ‘when-scrolled’ directive is to implement infinite scrolling, which is suppose to detect the div scroll.
when-scrolled="loadData(1)"
Here ‘loadData(1)’ callback function will get called while div/page is scrolled. In this method we have passed parameter value ‘1’ to detect, is it default data load or scroll load call.
$scope.loadData = function (IsPaging) {}
This is where we passed the parameter in our ngController method.
Finally the View:
We have declare directive ‘when-scrolled’ in our view, now we have to add directive in our module. In this app which is:
angular.module('uCRM_App', [])
The directive: This will detect the scroll position of page/div.
.directive('whenScrolled', function () { return function (scope, elm, attr) { var raw = elm[0]; elm.bind('scroll', function () { if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) { scope.$apply(attr.whenScrolled); } }); }; })
In out controller method we have pushed data into the array in scope to update the view.
for (model in data) { $scope.ListOffice.push(data[model]); }
As we know AngularJS will update the view when scope is changed/update.
Finally the Script:
angular.module('uCRM_App', []) .directive('whenScrolled', function () { return function (scope, elm, attr) { var raw = elm[0]; elm.bind('scroll', function () { if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) { scope.$apply(attr.whenScrolled); } }); }; }) .controller('OfficeController', function ($scope, $http) { $scope.loaderMore = false; $scope.scrollended = false; var IsPaging = 0; var UserID = 1; var page = 1; var PageSize = 20; var inCallback = false; var totalData = 0; //******=========Get All Data with Paging=========****** $scope.loadData = function (IsPaging) { var geturl = '/Crm/api/Office/Get/'; if (IsPaging === 1) { //debugger; IsPaging = 1; if (page > -1 && !inCallback) { inCallback = true; page++; $scope.loaderMore = true; $scope.LoadMessage = ' Loading page ' + page + ' of ' + PageSize + ' data...'; $scope.result = "color-green"; $http({ method: 'GET', url: geturl + UserID + '/' + page + '/' + PageSize + '/' + IsPaging, }) .success(function (data) { totalData = data.length; if (totalData === 0) { $scope.loaderMore = false; $scope.scrollended = true; $scope.LoadMessage = 'No more data...!'; $scope.result = "color-red"; Command: toastr["warning"]("No more data...!"); inCallback = false; page = -1; } else { for (model in data) { $scope.ListOffice.push(data[model]); } $scope.loaderMore = false; inCallback = false; } }).error(function (error) { alert('Error Loading..'); }) } }//unlimited load data while scroll else { //debugger; IsPaging = 0; $scope.loaderMore = true; $scope.LoadMessage = ' Loading page ' + page + ' of ' + PageSize + ' data...'; $scope.result = "color-green"; $http({ method: 'GET', url: geturl + UserID + '/' + page + '/' + PageSize + '/' + IsPaging, }). success(function (data) { $scope.ListOffice = data; $scope.loaderMore = false; }). error(function () { $scope.message = 'Unexpected Error while loading data!!'; console.log($scope.message); }); }//default load data while pageload }; $scope.loadData(); });
Output:
Happy Coding 🙂
Vishal says:
I am using “when-scrolled” for infinite paging but it’s not working. Can you help me.
VINAY GUPTA says:
hi Shashangka Shekhar,
Nice article,
“Can you provide me the full source code for it”
thanks for helping us
VINAY GUPTA says:
Email Id :
Shashangka Shekhar says:
Sure 🙂
srini says:
Could you also please send the source code to me
Arnab says:
Hi,
Its a nice article. I want to implement the same into my application. Can you provide me full source code for the same. My email id is
free love spells cast for me says:
Hi,I check your blogs named “ASP.Net MVC Infinite Scrolling With Angularjs | SHASHANGKA SHEKHAR’S BLOG” like every week.Your humoristic style is witty, keep up the good work! And you can look our website about free love spells cast for me.