angular - 在角度 4 中加载更多按钮
问题描述
从 10 个项目的 api 中,我想显示所有 10 个项目,并且在一个按钮中,单击时想要重复显示相同的所有 10 个项目,并且再次单击相同的 10 个项目作为重复
app.component.html
<div>
<ul *ngFor="let x of content">
<li>{{x.name}}</li>
</ul>
<li class="showmore">
<button class="load-more" (click)="getData()">
load more
</button>
</li>
</div>
app.component.ts
export class AppComponent {
constructor(private http: Http) { this.counter=0;
this.getData();
}
httpdata = [];
ngOnInit() {
this.http.get("http://jsonplaceholder.typicode.com/users")
.map((response) => response.json())
.subscribe((data) => { this.displaydata(data); } )
}
content:any[]=new Array();
counter:number;
displaydata(data) {this.httpdata = data;}
getData(){
console.log(this.counter + 'dat size'+this.httpdata.length)
for(let i=this.counter+1;i<this.httpdata.length;i++) {
this.content.push(this.httpdata[i]);
if(i%15==0) break;
}
this.counter+=5;
}
}
解决方案
export class AppComponent {
constructor(private http: Http) { this.counter=0;
this.getData();
}
httpdata = [];
ngOnInit() {
this.http.get("http://jsonplaceholder.typicode.com/users")
.map((response) => response.json())
.subscribe((data) => { this.displaydata(data); } )
}
content:any[]=new Array();
counter:number;
displaydata(data) {this.httpdata = data;}
getData(){
let content = [...this.content];
console.log(this.counter + 'dat size'+this.httpdata.length)
for(let i=this.counter+1;i<this.httpdata.length;i++) {
content.push(this.httpdata[i]);
if(i%15==0) break;
}
this.content = content;
this.counter+=5;
}
}
推荐阅读
- c - C API 使用 tz 数据库获取 DST 的开始和结束日期
- kirby - Kirby CMS:如何将块返回为 HTML?
- python-3.x - 正则表达式提取 Excel 单元格引用
- reactjs - 在 useEffect 中,如何在运行后续逻辑之前先正确更新状态?
- angular - Angular 材质 CdkDragPreview 输入
- python - 如何在 MultiHeadAttention 中为 TpyTorch 的 3d attn_mask 参数准备数据
- python - 在 plotly express 中添加另一个数据框作为注释
- flutter - Flutter:使用在多个屏幕中使用的提供程序更新特定屏幕
- reactjs - useState 设置方法不会立即反映更改
- performance - Enumerable.SequenceEqual(list1, list2) 相对于 list1.All(list2.Contains) 有什么优缺点?