首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular ngFor items by 5

Angular ngFor items by 5
EN

Stack Overflow用户
提问于 2020-04-06 19:58:37
回答 1查看 40关注 0票数 0

我有一个我不知道如何解决的问题。例如,当您打开一个网站时,单击“显示更多”时会显示5条评论,再次单击时会显示5条更多评论。现在,所有内容都在一次打印完成。也许你们知道怎么处理这件事?

代码:

代码语言:javascript
复制
    <div class="comment" *ngFor="let comment of comments ">
      <app-student-comment [comment]="comment"></app-student-comment>
    </div>

下面是我获取数据的方式:

代码语言:javascript
复制
  this.commentService.getStudentComments({applicationId: this.application.id}).subscribe(data => {
            this.comments = data;
          });

服务:

代码语言:javascript
复制
  getStudentComments({ applicationId }): Observable<Comment[]> {
    return this.httpClient.get<Comment[]>(`${this.proxyurl}${this.url}${this.apiPath}/applications/${applicationId}/comments/applicant/visible`);
  }
EN

回答 1

Stack Overflow用户

发布于 2020-04-06 20:09:56

在调用您的后台时,需要添加totalComments参数。

代码语言:javascript
复制
<div class="comment" *ngFor="let comment of comments ">
    <app-student-comment [comment]="comment"></app-student-comment>
</div>
<button (click)="addComments()">More</button>

在你的page.component.ts

代码语言:javascript
复制
public totalComments = 5;
public comments = [];

...

ngOnInit() {
    this.getComments();
}

public getComments() {
    this.commentService.getStudentComments({
        applicationId: this.application.id,
        totalComments: this.totalComments
    }).subscribe(data => {
         this.comments = data;
    });
}

public addComments() {
    this.totalComments += 5;
    this.getComments();
}

在你的服务中:

代码语言:javascript
复制
getStudentComments({ applicationId, totalComments }): Observable<Comment[]> {
    return this.httpClient.get<Comment[]>(
    `${this.proxyurl}${this.url}${this.apiPath}/applications/${applicationId}/comments/total/${totalComments}/applicant/visible`
    );
}

然后,您需要更新您的后端代码以使用此totalComments参数,例如,使用SQL查询:

代码语言:javascript
复制
this.db.query(`SELECT * FROM comments LIMIT $1`, [totalComments]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61059304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档