Jam*_*lle 2 javascript infinite-scroll angularjs nginfinitescroll
我想在我的Angular 4项目中添加一个带有ngx-infinite-scroll的无限滚动.
阵列数据有大约800个来自API的帖子.
最初,我想显示20个帖子,每次滚动页面时,都会显示20个帖子.
目前,每当我向下滚动时,我都可以看到控制台日志消息(滚动!).
但是当我滚动它时,我无法弄清楚如何将20个帖子附加到表中.
这是我正在尝试的代码.
onScrollDown函数
onScrollDown(){
this.dataService.getPosts().subscribe((posts)=>{
for (let post of posts){
let data = '<tr><td>'+ post.title +'</td><td>'+ post.geo +'</td><td>'+ post.Telephone +'</td><td>'+ post.category +'</td><td>Detail</td></tr>';
$('table.feed tbody').append(data);
}
});
Run Code Online (Sandbox Code Playgroud)
.
这是组件代码.
posts.component.html
<div *ngIf="posts?.length > 0;else noPosts" class="search-results" infinite-scroll [infiniteScrollDistance]="2" [infiniteScrollUpDistance]="2" [infiniteScrollThrottle]="50" (scrolled)="onScrollDown()" [scrollWindow]="false">
<table class="responsive-table feed striped">
<thead>
<tr>
<th>Name</th>
<th>State/City</th>
<th>Phone</th>
<th>Category</th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let post of posts | filter:term">
<td>{{post.title}}</td>
<td>{{post.geo}}</td>
<td>{{post.Telephone}}</td>
<td>{{post.category}}</td>
<td>Detail</td>
</tr>
</tbody>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
posts.component.ts
import { Component, OnInit } from '@angular/core';
import { DataService } from '../../services/data.service';
import { FilterPipe } from '../../filter.pipe';
declare var jquery:any;
declare var $ :any;
@Component({
selector: 'feed',
templateUrl: './feed.component.html',
styleUrls: ['./feed.component.css']
})
export class FeedComponent implements OnInit {
term : '';
posts: Post[];
constructor(private dataService: DataService) { }
ngOnInit() {
this.dataService.getPosts().subscribe((posts)=>{
this.posts = posts.slice(0,10);
});
}
onScrollDown(){
console.log("scrolled!");
}
interface Post{
id:number,
title:string,
contact:string,
Address:string,
Telephone:number,
Email:string,
Website:string,
Establishment:string,
sector:string,
category:string,
}
Run Code Online (Sandbox Code Playgroud)
小智 6
首先,像这样保存您的原始数组
this.dataService.getPosts().subscribe((response)=>{
this.originalPosts = response;
this.posts = response.slice(0,20);
});
onScrollDown(){
if(this.posts.length < this.originalPosts.length){
let len = this.posts.length;
for(i = len; i <= len+20; i++){
this.posts.push(this.originalPosts[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
只需将它推到同一个数组上,你就不需要将它直接附加到表中,angular会自行管理,使用angular时太容易了.
| 归档时间: |
|
| 查看次数: |
13086 次 |
| 最近记录: |