AngularJS: Infinite Scrolling
by Danny Michaelis
HTML
<script src="http://code.angularjs.org/1.0.0rc9/angular-1.0.0rc9.js"></script>
<div>
<ul id="fixed" when-scrolled="loadMore()">
<li ng-repeat="i in items">{{i.id}}</li>
</ul>
</div>
CSS
li {
height: 10px;
border-bottom: 1px solid gray;
}
#fixed {
max-height: 200px;
overflow: auto;
}
JavaScript
function Main($scope) {
$scope.items = [];
var counter = 0;
$scope.loadMore = function() {
for (var i = 0; i < 5; i++) {
$scope.items.push({id: counter});
counter += 10;
}
};
$scope.loadMore();
}
angular.module('scroll', []).directive('whenScrolled', function() {
return function(scope, elm, attr) {
var raw = elm[0];
console.log(raw.scrollTop, raw.offsetHeight, raw.scrollHeight)
elm.bind('scroll', function() {
console.log(raw.scrollTop, raw.offsetHeight, raw.scrollHeight)
if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) {
scope.$apply(attr.whenScrolled);
}
});
};
});