簡體   English   中英

事件發生后Angular 5更新模板

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM