在我的ThreadService类中,我有一个函数getThreads(),它返回一个Observable< Thread []>用我所有的线程.
现在,我希望我的函数的另一个版本,我的线程被选定的主题过滤:function getSelectedThemeThreads(theme:Theme).
我尝试使用运算符映射和过滤器但是我有以下错误消息属性’主题’在类型’Thread []上不存在.
在我正在处理的代码下面:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Thread } from '../models/thread.model';
import { Theme } from '../models/theme.model';
@Injectable({
providedIn: 'root'
})
export class ThreadService {
private threadsUrl = 'api/threads';
constructor(private http: HttpClient) { }
getThreads(): Observable<Thread[]> {
return this.http.get<Thread[]>(this.threadsUrl);
}
getSelectedThemeThreads(): Observable<Thread[]> {
return this.http.get<Thread[]>(this.threadsUrl).pipe(
map(threads => threads),
filter(thread => thread.theme.id === theme.id)
);
}
在此先感谢您的帮助.
解决方法:
我举了一个这个StackBlitz / angular6-filter-result的例子
主要思想是在map()中过滤,因为过滤器将获得一个对象数组.
getSelectedThemeThreads(theme: string): Observable<Flower[]> {
return this.http.get<Flower[]>(this.threadsUrl).pipe(
map(result =>
result.filter(one => one.theme === theme)
)
)
}