首页 > 解决方案 > 将 ion-item 添加到先前的 ion-item 而不是替换它

问题描述

我从带有分页的 api 获取问题列表,我有一个按钮,它运行一个函数并获取下一页问题。我需要添加这个之前的 ion-item 而不是替换它,这是我的代码:

home.html:

<div *ngFor="let question of this.questions?.data">
<ion-item class="questions_div" (click)="goToConsult(question.id);">
<p text-wrap>{{question.title}}</p> 
<span>{{question.created_at | jalali}}</span>
</ion-item>
</div>

现在发生的是它得到第二页并用第一页替换它,我需要将它添加到第一个离子项下面

home.ts=>

loadMore(currentPage){
this.nextPage = ++this.currentPage;
this.showLatestQuestions(this.nextPage);}

showLatestQuestions(currentPage){
this.questionsProvider.getLatestQuestions(currentPage)
.then(data => {
  this.questions = data;
  this.currentPage = this.questions.current_page;
  return this.currentPage;
})
.catch(error => alert(error)); }

标签: angulartypescriptionic-framework

解决方案


只需将您的新问题推送到现有数组中,而不是像这样分配新数据 -

 showLatestQuestions(currentPage){
this.questionsProvider.getLatestQuestions(currentPage)
.then(data => {
  for(let i=0; i< data.data.length;i++){
     this.questions.push(data.data[i]);
  }
  this.currentPage = this.questions.current_page;
  return this.currentPage;
})
.catch(error => alert(error)); 
}

推荐阅读