angularjs实现下拉加载更多数据,
我是通过指令来实现的,直接上代码
.directive("scroll", ["$window", "$document",function ($window, $document) { return function(scope, element) { angular.element($window).bind("scroll", function() { var pageYOffset = $window.pageYOffset; var clientHeight = $document[0].documentElement.clientHeight; var offsetHeight = $document[0].body.offsetHeight; //当滚动到90%的时候去加载 if(pageYOffset+clientHeight>offsetHeight*0.9) { //scope.shopWorkCanLoad是否可加载,controller中定义 //scope.shopWorkOnLoad是否正在加载,controller中定义 if(scope.shopWorkCanLoad==true && scope.shopWorkOnLoad==false){ //加载数据,controller中定义 scope.loadShopWork();// } } }); }; }]);
就这么多
用法我就不用再说了吧
2018年4月14日19:19:44更新
1.加载更多的函数可以传入
2.可以绑定到div的滚动监听上面
<div class="classContent" scroll has-more="hasMore" on-loading="onLoading" load-more="getMoreList()"> </div>
.directive("scroll", ["$window", "$document",function ($window, $document) { return { scope: {//=将ngModel同指定对象绑定 &将引用传递给这个方法 @储存与fromName相关联的字符串 hasMore: '=', // 将hasMore同指定对象绑定 onLoading: '=', // 将onLoading同指定对象绑定 loadMore: '&', // 将引用传递给这个方法 }, link:function(scope, element) { angular.element(element).bind("scroll", function(event) { let _element=event.target; var {scrollHeight,scrollTop,clientHeight} = _element; if(scrollTop+clientHeight>scrollHeight*0.9){ if(scope.hasMore==true && scope.onLoading==false){ scope.loadMore(); } } }); } }; }]);