我通过无限滚动调用更多项目,但是如果最初加载的项目没有填满屏幕,它将失败,我的意思是在这种情况下:
document.body.scrollHeight == document.body.clientHeight
如果页面具有滚动条,则该指令可以正常工作:
app.directive('infinitescroll', function ($rootScope, $http, $window) {
return function ($scope, $element) {
angular.element($window).bind('scroll', function() {
// 100 pixels gap at the bottom of page for triggering the fetcher
clientHeight = html.scrollHeight - 100;
scrollHeight = html.scrollTop + document.querySelector('body').clientHeight;
if (clientHeight <= scrollHeight) {
fetcher ();
}
});
function fetcher () {
//get the Id of last item >> lastId
$http.get(app.apiBaseUrl + '/items/' + lastId).then(function (response) {
// now pushing new items to $scope
});
}
}
});
我可以检查控制器是否
document.body.scrollHeight >= document.body.clientHeight
否则需要更多的项目,但是在控制器中检查scrollheight是一个不好的做法,请告诉我如何在上述指令中进行检查。
我想建议为提取程序创建Service,并将其注入指令中。您的提取程序可以具有一个函数,您可以在其中传递id,它会向后端发出请求并返回响应,然后可以将其注入到指令(关于指令)中:您可以创建一个限制为A的指令并将其设置为body属性像这样:
<body infinitescroll>
这样,您将拥有element
已包装在jQuery中的in指令,并且document.querySelector('body')
在计算后无需这样做,您可以从服务器获取数据并将其发送到您需要的控制器,方法是通过$emit
或$boradcast
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句