[英]Angular 5 update template after event
這是我關於角度的第三篇文章。 我想顯示一個候選類型列表:Candidate []。 我目前正在使用* ngFor在視圖中顯示它,如下所示:
<ul>
<li *ngFor="let candidate of candidates">
{{ candidate.name }}
</li>
</ul>
但是,我需要過濾候選人以使用戶搜索具有以下功能:
filterResults(queryString): void {
this.candidatesService.filterCandidates(queryString).subscribe(candidates => this.filteredCandidates = candidates);
this.filteredCandidates = this.filteredCandidates.filter(candidate => candidate.name == queryString);
}
問題是,該功能是在輸入框的鍵盤上觸發的。 因此,過濾后的候選數組值根據輸入而變化。
有人知道我也如何更新模板視圖,以便僅在更新的數組中僅顯示新結果嗎?
在*ngFor
創建temp並將您的候選者存儲在temp[]
,該條件在不搜索條件時和在this.filteredCandidates
過程中使用時將得到this.filteredCandidates
並將其推入temp[]
並顯示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.