简体   繁体   English

事件发生后Angular 5更新模板

[英]Angular 5 update template after event

This is my third post on angular. 这是我关于角度的第三篇文章。 I want to display a list of type candidates: Candidate[]. 我想显示一个候选类型列表:Candidate []。 I am currently displaying it in the view using an *ngFor like this: 我目前正在使用* ngFor在视图中显示它,如下所示:

    <ul>
        <li *ngFor="let candidate of candidates">
            {{ candidate.name }}
        </li>
    </ul>

However, i need to filter the candidates to fit the user search with a function : 但是,我需要过滤候选人以使用户搜索具有以下功能:

  filterResults(queryString): void {
    this.candidatesService.filterCandidates(queryString).subscribe(candidates => this.filteredCandidates = candidates);
    this.filteredCandidates = this.filteredCandidates.filter(candidate => candidate.name == queryString);
  }

The problem is, the function is triggered on keyup of an input box. 问题是,该功能是在输入框的键盘上触发的。 So, the filtered candidates array value is changing based on input. 因此,过滤后的候选数组值根据输入而变化。

Does anybody know how do i update the template view too, to only display the newly only the results in the updated array ? 有人知道我也如何更新模板视图,以便仅在更新的数组中仅显示新结果吗?

*ngFor创建temp并将您的候选者存储在temp[] ,该条件在不搜索条件时和在this.filteredCandidates过程中使用时将得到this.filteredCandidates并将其推入temp[]并显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM