我对爱奥尼亚并不熟悉,用它来实现它总是一个很好的选择
pipes
. 你可以在网上找到任何教程。这里我有另一个解决方案
在此模板中
search()
功能将在每次按键时触发。
<input name="search" type="text" placeholder="Search" (keyup)="search($event)">
在构造函数中
this.participantsList = afDatabase.list('/paticipants');
this.participants = this.participantsList.valueChanges();
当页面加载时,上述代码将首先加载模板中的所有数据。在组件中添加以下功能,该功能将过滤每个按键上的数据
search(event){
//console.log(event.target.value);
let term = (<HTMLInputElement>event.target).value;
this.participants = this.participantsList.valueChanges().map(res=> res.filter(guest => guest.name.toLowerCase().includes(term.toLowerCase())))
}
并在模板中显示数据
<ng-container *ngIf="participants | async; let participantsItems; else nocontent">
<div *ngFor="let participant of participantsItems">
{{participant | json}}
</div>
<p>Search Results {{participantsItems.length}}</p>
</ng-container>
<ng-template #nocontent>No Search Result...</ng-template>