Asp.net web api 用开关映射观察的Angular2滤波器
我正在使用Angular2和WebAPI进行术语搜索,使用的方法在许多Angular2教程中都有描述 为我服务:Asp.net web api 用开关映射观察的Angular2滤波器,asp.net-web-api,angular,typescript,Asp.net Web Api,Angular,Typescript,我正在使用Angular2和WebAPI进行术语搜索,使用的方法在许多Angular2教程中都有描述 为我服务: search(term: string): Observable<UserList[]> { return this.http.get(this.userSearchUrl + term) .map((response: Response) => response.json()) .catch(this.handleError
search(term: string): Observable<UserList[]> {
return this.http.get(this.userSearchUrl + term)
.map((response: Response) => response.json())
.catch(this.handleError);
}
搜索(术语:字符串):可观察{
返回this.http.get(this.userSearchUrl+term)
.map((response:response)=>response.json())
.接住(这个.把手错误);
}
在我的组件中:
ngOnInit(): void {
this.users = this.searchTermStream
.debounceTime(300)
.distinctUntilChanged()
.switchMap((term: string) =>
term ? this.userService.search(term) : Observable.of<UserList[]>([]))
.catch(error => {
console.log(error);
return Observable.of<UserList[]>([]);
});
}
ngOnInit():void{
this.users=this.searchTermStream
.debounceTime(300)
.distinctUntilChanged()
.switchMap((术语:字符串)=>
术语?this.userService.search(术语):可观察的([]))
.catch(错误=>{
console.log(错误);
([])的可观测收益率;
});
}
我想在服务端做的是跟踪上次搜索。因此,如果我的初始搜索是“b”,然后我搜索“bo”,你不必再调用WebAPI,因为我们已经得到了需要的结果,我们只需要进一步过滤。我假设用可观测数据做这件事是不可能的/困难的,因为你订阅了一个流,但我对可观测数据还不太了解。我想用承诺来做可能会更容易,但我不确定如何处理它的组件端,比如Subject()和switchMap(),它们似乎只用于可观察对象
这与我希望在服务端使用的类似:
searchPromise(term: string): Promise<UserList[]> {
if (this.lastUsedTerm.length > 0 && term.indexOf(this.lastUsedTerm) == 0) {
this.lastUsedSearch = this.lastUsedSearch.then(x => x.filter(z => z.firstName.startsWith(term)));
}
else {
this.lastUsedSearch = this.http.get(this.userSearchUrl + term)
.toPromise()
.then((response: Response) => response.json())
.catch(this.handleError);
}
this.lastUsedTerm = term;
return this.lastUsedSearch.then(x => x.filter(z => z.firstName.startsWith("bob")));
}
searchPromise(术语:字符串):Promise{
如果(this.lastUsedTerm.length>0&&term.indexOf(this.lastUsedTerm)==0){
this.lastUsedSearch=this.lastUsedSearch.then(x=>x.filter(z=>z.firstName.startsWith(term));
}
否则{
this.lastUsedSearch=this.http.get(this.userSearchUrl+术语)
.toPromise()
.then((response:response)=>response.json()
.接住(这个.把手错误);
}
this.lastUsedTerm=术语;
返回这个.lastUsedSearch.then(x=>x.filter(z=>z.firstName.startsWith(“bob”));
}