简体   繁体   English

使用RxJs switchMap提前角输入

[英]Angular typeahead with RxJs switchMap subscribe

Using Angular bootstrap Typeahead plugin to show colours. 使用Angular bootstrap Typeahead插件显示颜色。 But I need to resolve the circular references like in the other places I used. 但是我需要像在其他地方一样解决循环引用。 I cant make it with RxJs SwitchMap operator. 我无法使用RxJs SwitchMap运算符来实现。 Is there any other possible way to resolve? 还有其他可能的解决方法吗?

(<any>window).resolveJsonReferences(this.colorService.searchColors(term))

Typeahead with RxJs SwitchMap 使用RxJs SwitchMap提前输入

 search = (text$: Observable<string>) =>
    text$
    .debounceTime(200)
    .distinctUntilChanged()
    .do(() => this.searching = true)
    .switchMap(term =>
        this.colorService.searchColors(term) // Json result needs circular reference resolver
        .do(() => this.searchFailed = false)
        .catch(() => {
            this.searchFailed = true;
            return Observable.of([]);
        }))
    .do(() => this.searching = false);

In Other places 在其他地方

return this.colorService.searchColors(term)
        .subscribe(
        res => {               
            this.colors= this.utilities.resolveJsonReferences(res);
        },
        err => {               
        });

Just map the value through your resolver. 只需通过解析器map值即可。

.map((term) => this.utilities.resolveJsonReferences(term))

All together: 全部一起:

search = (text$: Observable<string>) =>
    text$
    .debounceTime(200)
    .distinctUntilChanged()
    .do(() => this.searching = true)
    .switchMap(term =>
        this.colorService.searchColors(term) // Json result needs circular reference resolver
        .map((term) => this.utilities.resolveJsonReferences(term))
        .do(() => this.searchFailed = false)
        .catch(() => {
            this.searchFailed = true;
            return Observable.of([]);
        }))
    .do(() => this.searching = false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM