angular - 如何以角度过滤列表
问题描述
我有一个项目列表(listUsers),我想过滤列表。但它不起作用。我想知道我的方法是否正确?
这是 html 文件:
<div class="search-div">
<mat-form-field class="search-form-field" floatLabel="never" style="display: flex;`justify-content: center;">
<input matInput [(ngModel)]="searchKey" placeholder="Search" autocomplete="off" (keyup)="applyFilter()">
<button mat-button matSuffix mat-icon-button aria-label="Clear" *ngIf="searchKey" (click)="onSearchClear()">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
<mat-selection-list >
<mat-list-option *ngFor="let d of listUsers" >{{d.username}}</mat-list-option>
</mat-selection-list>
</div>
这是 .ts 文件
listUsers: any;
ngOnInit() {
this.userService.getAll()
.subscribe(data => {
this.listUsers = data;
console.log(this.listUsers);
});
}
onSearchClear() {
this.searchKey = '';
}
applyFilter(value: string) {
this.listUsers.filter = this.searchKey.trim().toLowerCase();
}
解决方案
它不是“角度过滤功能”,而是 javascript 数组过滤功能。您需要传递一个回调。
https://www.w3schools.com/jsref/jsref_filter.asp
例子:
this.listUsers = this.listUsers.filter(user => user === condition)
推荐阅读
- angular - nativescript-contacts 如何与谷歌联系人同步?
- c# - EF Core 2.1 身份表在 HasData() 播种后为空
- c - C 中变音符号“A”的用途或等价物是什么?
- data-warehouse - 一对多查找,转换为一对一维度
- c++ - 文件 I/O 二进制动态数组崩溃
- c# - 为什么 LINQ orderby 会消耗更多内存?
- wordpress - Group by,Order by 在 MySQL 但不是 MariaDB
- xamarin.ios - Xamarin.iOS:Google 广告未在我的设备上显示
- github - 如何将 index.html 移动到 Github 的分支 gh-pages 的顶部?
- python - 将数组展平为列表列表