首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Angular:文本搜索rxjs问题

Angular:文本搜索rxjs问题
EN

Stack Overflow用户
提问于 2018-09-06 03:01:46
回答 1查看 80关注 0票数 3

我在Angular 6中创建了一个搜索。我有一个艰难的时间与我的流。

我希望我的rx流:

  1. 接受带有subject的文本输入

,该Call告诉流在单击按钮时获得更多结果(增量限制),并调用

服务

我所拥有的一切都是有效的,除了:

当主体发出时,它什么也不做,我希望switchmap会介入,从而得到服务器response..

  • How。每次subject发出时,我可以将限制增加10吗?

searchInput: FormControl =新主题();showMore$:FormControl=新主题();ngOnInit() { this.searchInput .valueChanges .pipe( debounceTime(300),distinctUntilChanged(),withLatestFrom(this.showMore$.pipe(startWith(10),tap(val => {//如何在此处增加值?}),// showMore$发出...switchMap((searchTerm,limit) => { return this.myservice.search(searchTerm,limit) }),) .subscribe(response => { this.results = response;});} showMore():void { this.showMore$.next(10);}

EN

回答 1

Stack Overflow用户

发布于 2018-09-06 03:18:29

据我所知,switchMap应该可以按原样工作,尽管您可能希望在其中添加一些错误处理。

要将该值增加10,只需将该值放入存储中并触发一个操作,该操作将触发减法器在下一次循环中将其增加10。

代码语言:javascript
复制
.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语句来记录值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52191839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档