我试图在 Angular 中为我的材料表添加一个过滤器选项,但我收到了这两个错误,它们在“值”上指定:
Object is possibly 'null'.
Property 'value' does not exist on type 'EventTarget'.
这是我的 .html 文件:
<mat-form-field>
<input (keyup)="applyFilter($event.target.value)" matInput placeholder="Filter">
</mat-form-field>
和 .ts 文件:
applyFilter(filterValue: string) {
this.dataSource.filter = filterValue.trim().toLowerCase();
}
我做错了什么/我应该做些什么不同的?
答案 0 :(得分:1)
添加 id
(#filter) 以使用 filter.value
输入和检索值
<input matInput #filter (keyup)="applyFilter(filter.value)" placeholder="Filter">
添加另一个函数来处理事件并将值解析为 applyFilter
。
.component.ts
fireFilterEvent(event: Event) {
const input = (event.target as HTMLInputElement).value;
this.applyFilter(input);
}
对于您的 input
元素,您在 fireFilterEvent($event)
事件上调用 (keyup)
。
.component.html
<input matInput (keyup)="fireFilterEvent($event)" placeholder="Filter">