我为我的 Angular 应用程序创建了一个模块,并且我有两个相似的指令与它们相关联。
当滚动到页面底部时触发一个(whenScrolled)进行无限分页。另一个(fromTop)触发“顶部”或“底部”,可用于隐藏/显示页面上的“转到顶部”链接。这就是我设置它们的方式:
angular.module('scroll', []).directive('fromTop', function() {
return function(scope, element, attributes) {
var raw = element[0]
var scrollDistance = Math.round(window.outerHeight)
window.onscroll = function() {
console.log(window.pageYOffset, raw.scrollHeight + scrollDistance)
if (window.pageYOffset >= raw.scrollHeight + scrollDistance) {
scope.position = 'bottom'
scope.$apply(attributes.fromTop)
} else {
scope.position = 'top'
scope.$apply(attributes.fromTop)
}
}
}
}).directive('whenScrolled', function() {
return function(scope, element, attributes) {
var raw = element[0]
var scrollDistance = Math.round(window.outerHeight)
window.onscroll = function() {
//console.log(window.pageYOffset, raw.scrollHeight - scrollDistance)
if (window.pageYOffset >= raw.scrollHeight - scrollDistance) {
scope.$apply(attributes.whenScrolled)
}
}
}
});
不幸的是,这不起作用。似乎“whenScrolled”指令覆盖了“fromTop”指令,并且 fromTop 永远不会被调用。但是,如果我删除“whenScrolled”,“fromTop”就会被调用。为什么是这样?