我需要根据选择筛选数据,因为选项太多。 我是这样做的:
<mat-form-field>
<mat-select placeholder="Unit.." (change)="onTabChange(element, $event.value.id, 'assignedUnit')">
<input matInput type="text" (keyup)="filterListCareUnit($event.target.value)">
<mat-option *ngFor="let careUnit of listCareUnits" [value]="careUnit">
{{ careUnit.label }}
</mat-option>
</mat-select>
</mat-form-field>
按键时,我正在调用 filterListCareUnit($event.target.value)
但是在这个函数中我不知道使用过滤器 rxjs
我有我的 listCareUnits
,我想删除所有不包含 $event.target.value
现在我这样做了,但显然行不通,我的列表总是包含相同的项目:
filterListCareUnit(val) {
console.log(val);
this.listCareUnits.filter((unit) => unit.label.indexOf(val));
}
angular/rxjs 的新手 .. 感谢您的帮助
最佳答案
您必须分配返回的过滤列表。
filterListCareUnit(val) {
console.log(val);
this.listCareUnits = this.listCareUnits.filter((unit) => unit.label.indexOf(val) > -1);
}
但是为了不丢失初始数组的数据,最好使用另一个始终包含初始数据集的数组进行处理。
anotherArray = this.listCareUnits;
filterListCareUnit(val) {
console.log(val);
this.listCareUnits = this.anotherArray.filter((unit) => unit.label.indexOf(val) > -1);
}
关于angular - 在垫选择上过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50645353/