我试着用这种方法过滤一个自动完成的垫子:
ngOnInit 函数:
filterEmployee() {
this.filteredOptions = this.employee.valueChanges.pipe(
startWith(""),
map((value) => this._filter(value))
);
}滤波函数
private _filter(value: string): Observable<Employee[]> {
const filterValue = value && value.toLowerCase();
return of(
this.employees.filter(
(employee) =>
employee.name.toLowerCase().indexOf(filterValue) > 1
)
);
}有个错误:

我做错了什么?
发布于 2022-03-18 12:51:24
您尝试返回可观察到的字符串数组,但您已经编写了可观察到的员工。您可以像这样更新Observable:
private _filter(value: string): Observable<string[]> {
const filterValue = value && value.toLowerCase();
return of(
this.employees.filter(
(employee) =>
employee.name.toLowerCase().indexOf(filterValue) > 1
)
);
}发布于 2022-03-18 12:52:44
首先,不要map()到一个Observable (我的意思是,你可以,但这不是你想要的)。我想你把map和flatMap搞混了。
private _filter(value: string): Employee[] {
const filterValue = value && value.toLowerCase();
return employees.filter(
(employee) =>
employee.name.toLowerCase().indexOf(filterValue) > 1
);
}现在您将分配Observable<Employee[]>而不是Observable<Observable<Employee[]>>
我们能看到this.employees和Employee的定义吗?
https://stackoverflow.com/questions/71527256
复制相似问题