Today we are going to know about infinite/unlimited scrolling in AngularJS.

Infinite/unlimited scroll is while we scroll down our web page it’ll automatically load next paged data. Facebook has this type of 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 supposed 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:

  1. <div class="widget-content content_scroll" when-scrolled="loadData(1)">
  2. <table class="table table-striped table-bordered table-hover table-checkable datatable">
  3. <thead>
  4. <tr>
  5. <th>Office ID</th>
  6. <th>Office Code</th>
  7. <th>Office Name</th>
  8. <th>Is Active</th>
  9. <th>Create Date</th>
  10. <th>Create By</th>
  11. <th>Edit Date</th>
  12. <th>Edit By</th>
  13. <th>Status</th>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. <trng-repeat="dataModel in ListOffice">
  18. <td>{{dataModel.OfficeID}}</td>
  19. <td>{{dataModel.OfficeCode}}</td>
  20. <td>{{dataModel.OfficeName}}</td>
  21. <td>{{dataModel.IsActive}}</td>
  22. <td>{{dataModel.CreateDate|date:"MM/dd/yyyy 'at' h:mma"}}</td>
  23. <td>{{dataModel.CreateBy}}</td>
  24. <td>{{dataModel.EditDate|date:"MM/dd/yyyy 'at' h:mma"}}</td>
  25. <td>{{dataModel.EditBy}}</td>
  26. <td>
  27. <span class="label label-warning">
  28. <a href="javascript:void(0);" class="bs-tooltip" title="Edit" ng-click="getOffice(dataModel)">
  29. <i class="icon-pencil"></i>
  30. </a>
  31. </span>
  32. <span class="label label-danger">
  33. <a href="javascript:void(0);" class="bs-tooltip" title="Delete" ng-click="deleteOffice(dataModel)">
  34. <i class="icon-trash"></i>
  35. </a>
  36. </span>
  37. </td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <div class="loadmore">
  42. <div ng-show="loaderMore" ng-class="result">
  43. <imgsrc="~/Areas/Crm/Content/img/ng-loader.gif" />{{LoadMessage}}
  44. </div>
  45. <div ng-show="scrollended" ng-class="result">
  46. {{LoadMessage}}
  47. </div>
  48. </div>
  49. </div>
We have declared 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.
  1. .directive('whenScrolled', function()
  2. {
  3. return function(scope, elm, attr)
  4. {
  5. var raw = elm[0];
  6. elm.bind('scroll', function()
  7. {
  8. if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight)
  9. {
  10. scope.$apply(attr.whenScrolled);
  11. }
  12. });
  13. };
  14. })
In our controller method we have pushed data into the array in scope to update the view.
  1. for (model in data)
  2. {
  3. $scope.ListOffice.push(data[model]);
  4. }
As we know AngularJS will update the view when scope is changed/update.

Finally the Script:
  1. angular.module('uCRM_App', [])
  2. .directive('whenScrolled', function()
  3. {
  4. return function(scope, elm, attr)
  5. {
  6. var raw = elm[0];
  7. elm.bind('scroll', function()
  8. {
  9. if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight)
  10. {
  11. scope.$apply(attr.whenScrolled);
  12. }
  13. });
  14. };
  15. })
  16. .controller('OfficeController', function($scope, $http)
  17. {
  18. $scope.loaderMore = false;
  19. $scope.scrollended = false;
  20. var IsPaging = 0;
  21. var UserID = 1;
  22. var page = 1;
  23. var PageSize = 20;
  24. var inCallback = false;
  25. var totalData = 0;
  26. //******=========Get All Data with Paging=========******
  27. $scope.loadData = function(IsPaging)
  28. {
  29. vargeturl = '/Crm/api/Office/Get/';
  30. if (IsPaging === 1)
  31. {
  32. //debugger;
  33. IsPaging = 1;
  34. if (page > -1 && !inCallback)
  35. {
  36. inCallback = true;
  37. page++;
  38. $scope.loaderMore = true;
  39. $scope.LoadMessage = ' Loading page ' + page + ' of ' + PageSize + ' data...';
  40. $scope.result = "color-green";
  41. $http({
  42. method: 'GET',
  43. url: geturl + UserID + '/' + page + '/' + PageSize + '/' + IsPaging,
  44. })
  45. .success(function(data)
  46. {
  47. totalData = data.length;
  48. if (totalData === 0)
  49. {
  50. $scope.loaderMore = false;
  51. $scope.scrollended = true;
  52. $scope.LoadMessage = 'No more data...!';
  53. $scope.result = "color-red";
  54. Command: toastr["warning"]("No more data...!");
  55. inCallback = false;
  56. page = -1;
  57. } else {
  58. for (model in data)
  59. {
  60. $scope.ListOffice.push(data[model]);
  61. }
  62. $scope.loaderMore = false;
  63. inCallback = false;
  64. }
  65. }).error(function(error)
  66. {
  67. alert('Error Loading..');
  68. })
  69. }
  70. } //unlimited load data while scroll
  71. else {
  72. //debugger;
  73. IsPaging = 0;
  74. $scope.loaderMore = true;
  75. $scope.LoadMessage = ' Loading page ' + page + ' of ' + PageSize + ' data...';
  76. $scope.result = "color-green";
  77. $http({
  78. method: 'GET',
  79. url: geturl + UserID + '/' + page + '/' + PageSize + '/' + IsPaging,
  80. }).
  81. success(function(data)
  82. {
  83. $scope.ListOffice = data;
  84. $scope.loaderMore = false;
  85. }).
  86. error(function()
  87. {
  88. $scope.message = 'Unexpected Error while loading data!!';
  89. console.log($scope.message);
  90. });
  91. } //default load data while pageload
  92. };
  93. $scope.loadData();
  94. });
Output

output