在Angular 5+中使用无限滚动,可以通过以下步骤实现:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
data: any[] = [];
constructor(private http: HttpClient) { }
ngOnInit() {
this.loadData();
}
loadData() {
this.http.get('your_api_url').subscribe((response: any) => {
this.data = response;
});
}
请将'your_api_url'替换为你实际的API地址。
<div *ngFor="let item of data">
<!-- 显示数据内容 -->
</div>
<div class="scroll-container" (scroll)="onScroll()">
<div *ngFor="let item of data">
<!-- 显示数据内容 -->
</div>
</div>
onScroll() {
const container = document.querySelector('.scroll-container');
const scrollHeight = container.scrollHeight;
const scrollTop = container.scrollTop;
const clientHeight = container.clientHeight;
if (scrollHeight - scrollTop === clientHeight) {
// 当滚动到底部时加载更多数据
this.loadMoreData();
}
}
loadMoreData() {
this.http.get('your_api_url').subscribe((response: any) => {
this.data = this.data.concat(response);
});
}
同样,请将'your_api_url'替换为你实际的API地址。
通过以上步骤,你就可以在Angular 5+中实现无限滚动功能了。当用户滚动到容器底部时,会自动加载更多数据。你可以根据实际需求自定义滚动容器的样式和加载数据的API。
领取专属 10元无门槛券
手把手带您无忧上云