Skip to content

Instantly share code, notes, and snippets.

@amitparrikar
Last active June 14, 2017 10:01
Show Gist options
  • Select an option

  • Save amitparrikar/bbd52f7fe2c6528fc37974e197ccc84b to your computer and use it in GitHub Desktop.

Select an option

Save amitparrikar/bbd52f7fe2c6528fc37974e197ccc84b to your computer and use it in GitHub Desktop.
An AngularJS directive called InfiniteScroll; Infinite Scrolling mechanism is used in lazy loading. It gets a callback handler which then gets called when scrolling has reached to the bottom of the element.
/**
* InfiniteScroll is a AngularJS directive.
* This directive calls your callback function when scrolling of an element is reached to the bottom.
*/
'use strict';
angular.module('InfiniteScroll', []).directive('infiniteScroll', function () {
return {
restrict: 'A',
scope: {
callback: '&infiniteScroll'
},
link: function($scope, iElem, iAttr){
/**
* Scroll callback event handler.
* @param {[type]} e [description]
* @return {[type]} [description]
*/
var _scrollHandler = function (e) {
if((e.target.scrollTop + e.target.clientHeight) >= e.target.scrollHeight){
$scope.callback({$event: e});
}
};
/**
* Attach the Scroll event to the element.
*/
iElem.on('scroll', _scrollHandler);
/**
* Unbind the scroll event when the scope is destroyed.
* @return {[type]} [description]
*/
$scope.$on('$destroy', function () {
iElem.off('scroll', _scrollHandler);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment