如何将RXJS运算符应用于FormGroup中的单个FormControl

发布于 2025-01-20 21:14:45 字数 318 浏览 3 评论 0原文

filterForm = this.fb.group({
    filterType: [''],
    filterValue:['']
    searchText:['']
  });
this.filterForm.valueChanges.subscribe(res=>{
      console.log(res); //some backend call
});

我想将一些 rxjs 运算符(例如 debounce、distinctUntilChanged)应用于 searchText 表单控制器,以便我可以使用整个表单值进行 api 调用。

filterForm = this.fb.group({
    filterType: [''],
    filterValue:['']
    searchText:['']
  });
this.filterForm.valueChanges.subscribe(res=>{
      console.log(res); //some backend call
});

I want to apply few rxjs operators like debounce, distinctUntilChanged only to searchText form controller so that I can make api call using the whole form value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2025-01-27 21:14:45

您可以通过选择进行控制和管道在订阅之前:

filterForm = this.fb.group({
    filterType: [''],
    filterValue:['']
    searchText:['']
  });
this.filterForm.get('searchText').valueChanges.pipe(
      distinctUntilChanged(),
      debounce(500),
)
.subscribe(res=>{
      console.log(res); //some backend call
});

You can do it with a get for select the control and a pipe before the subscribe:

filterForm = this.fb.group({
    filterType: [''],
    filterValue:['']
    searchText:['']
  });
this.filterForm.get('searchText').valueChanges.pipe(
      distinctUntilChanged(),
      debounce(500),
)
.subscribe(res=>{
      console.log(res); //some backend call
});
乖乖 2025-01-27 21:14:45

我认为最简单的方法是只用要应用运算符的一个字段,然后用当前表单值替换其值:

this.filterForm.get('searchText').valueChanges.pipe(
  .pipe(
    distinctUntilChanged(),
    debounce(N),
    map(() => this.filterForm.value),
  )
  .subscribe(res => {
    ...
  });

I think the easiest way is piping only the one field where you want to apply the operators and then replace its value with the current form value:

this.filterForm.get('searchText').valueChanges.pipe(
  .pipe(
    distinctUntilChanged(),
    debounce(N),
    map(() => this.filterForm.value),
  )
  .subscribe(res => {
    ...
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文