要追加新对象而不是替换之前的对象,可以在无限滚动的代码中使用 Angular 中的 NgZone 服务。
在组件的构造函数中注入 NgZone 服务:
import {Component, NgZone} from '@angular/core';
@Component({
selector: 'my-component',
template:
,
})
export class MyComponent {
items = [];
constructor(private zone: NgZone) {}
// 滚动条滚动到底部时,调用此函数 onScroll(event) { const element = event.target; if (element.scrollHeight - element.scrollTop === element.clientHeight) { this.zone.run(() => { // 在这里添加逻辑来追加新对象 }); } } }
然后在 NgZone 的 run() 方法中添加逻辑来追加新对象,如:
this.zone.run(() => { this.items.push(newItem); });
这样,每次触发滚动事件时,新对象都会被追加到已有对象的末尾,而不是替换之前的对象。
上一篇:Angular无脚本样式加载