当用户滚动或触底时,我试图在列表中添加更多项。我发出指令并检查滚动位置,如果用户到达底部,我使用
event emitter
.调用了我的函数,但它未在数组中添加项。
这是我的代码:
https://stackblitz.com/edit/angular-zsedct?file=src%2Fapp%2Fscroll.directive.ts
scroll = (e): void => {
//e.target.scrollTop = current scroll position
//e.target.scrollHeight - e.target.offsetHeight - to calculate the bottom. equivalent to scrollTopMax in FF
if (e.target.scrollTop == e.target.scrollHeight - e.target.offsetHeight) {
console.log('You reached the bottom!');
this.valueChange.next();
}
//handle your scroll here
//notice the 'odd' function assignment to a class field
//this is used to be able to remove the event listener
};
成分
addMoreElement(){
console.log('add more button')
for(var i=0;i<5;i++){
this.arr.push('hellp'+i)
}
}
有没有更好的方法来完成同样的任务?你能告诉我另一种方法来实现同样的事情,并做一些改进吗