[英]Angular: Text search rxjs issue
我在Angular 6
創建了一個搜索。 我的流很困難。
我想要我的rx流:
subject
組合,告訴流在單擊按鈕時獲得更多結果(增量限制) 我有什么作品,除了:
switchmap
能夠啟動,從而獲得服務器響應。 每次subject
發出10次時如何增加限制?
searchInput: FormControl = new FormControl(); showMore$: Subject<number> = new Subject(); ngOnInit() { this.searchInput .valueChanges .pipe( debounceTime(300), distinctUntilChanged(), withLatestFrom(this.showMore$.pipe(startWith(10), tap(val => { //how to increment the value here? }))), //switchmap not called when showMore$ emits... switchMap(([searchTerm, limit]) => { return this.myservice.search(searchTerm, limit) }), ) .subscribe(response => { this.results = response; }); } showMore(): void { this.showMore$.next(10); }
據我所知,switchMap應該按原樣運行,盡管你可能想在那里進行一些錯誤處理。
要將值增加10,只需將值放入存儲區並觸發一個操作,該操作將觸發reducers將其下一輪增加10。
.pipe(
debounceTime(300),
distinctUntilChanged(),
// get the value from the store using an ngrx selector
withLatestFrom(this.store.pipe(select(selectValue))),
switchMap(([searchTerm, limit]) => {
// essential to catchError else an HTTP error response will disable this
return this.myservice.search(searchTerm, limit).pipe(
catchError(() => {
return of('an error string which will be passed down the pipe on an error')
})
)
}),
tap(() => this.store.dispatch(new IncrementValueByTenAction()))
)
此外,使用大量的tap語句來記錄值,因為它們是通過管道傳遞的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.