【发布时间】:2020-12-26 17:45:02
【问题描述】:
试图过滤 Observable List 中的对象列表。
型号:
export class Month {
id: string;
description: string;
}
价值观:
描述:“一月” 编号:“01”
描述:“二月” 编号:“02”
描述:“三月” 编号:“03”
在我的服务中,我尝试返回带有所选值的列表,在这种情况下只有一个,但将来可能会更多。
selectedMonth: string = "02";
public getMonths(): Observable<Month[]> {
return of(this.MONTHS).pipe(
map(months => {
months.filter(month => {
month.id === this.selectedMonth;
});
return months;
})
);
}
但似乎过滤器根本不起作用,我总是得到整个列表(12 个元素,而不是一个元素)
ngOnInit(): void {
this.monthservice.getMonths().subscribe(months => (this.months = months));
console.log(this.months);
}
此链接有代码:
https://stackblitz.com/edit/angular-myfilter?file=src/app/services.ts/month.service.ts
有什么帮助吗?
谢谢
【问题讨论】:
标签: angular observable angular2-observables