简体   繁体   English

如何订阅反应式FormGroup并映射值更改(角度6)

[英]How to subscribe to a reactive FormGroup and map the value changes, in angular 6

I have a simple form in angular 6 that has only a textfield and a dropdown. 我在angular 6中有一个简单的表单,只有一个文本字段和一个下拉列表。 What I want is to observe that form and check for every change in that form. 我想要的是观察该表格并检查该表格中的所有更改。 So either a letter it is typed in the textfield or the dropdown changes, I want to call a function. 因此,无论是在文本字段中键入字母还是在下拉列表中进行更改,我都想调用一个函数。

The form group is like so 表单组是这样的

  nameForm = this.formBuilder.group({
    name:['',Validators.required],
    cepDrop:['construction']
  });

and my logic is 我的逻辑是

everytime a form value changes, wait a little until no other changes are made, grab the form object, map its contains into the function and then subscribe to this function's results. 每次表单值更改时,请稍等一会,直到没有其他更改为止,抓住表单对象,将其包含映射到函数中,然后订阅该函数的结果。

  ngOnInit() {
    this.nameForm.valueChanges
    //.debounceTime(400)
    //.distinctUntilChanged()
    .map( terms => this.mapcmsService.searchName(terms.name, terms.cepDrop))
    .subscribe( value => console.log(value));
  }

First of all debounceTime and distinctUntilChanged give the error Property 'debounceTime' does not exist on type 'Observable<any>'. 首先, debounceTimedistinctUntilChanged给出错误消息, Property 'debounceTime' does not exist on type 'Observable<any>'.

The main problem is that map also gives the same error. 主要问题是map也给出相同的错误。 Property 'map' does not exist on type 'Observable<any>'.

What can I do, so I can fix this? 我该怎么办,以便解决此问题? How to observe the whole form group and the map its values to a function ? 如何观察整个表单组并将其值映射到函数?

Thank you 谢谢

That's the old way of doing rxjs. 那是做rxjs的旧方法。 Now you're supposed to pass extra operators to the pipe operator like this: 现在,您应该像这样将额外的运算符传递给pipe运算符:

import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';

...

this.nameForm.valueChanges.pipe(
    debounceTime(400),
    distinctUntilChanged(),
    map(terms => this.mapcmsService.searchName(terms.name, terms.cepDrop))
).subscribe( value => console.log(value));

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

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