我有一个 react 形式,带有文本输入。为了便于在 typescript 中访问,我声明:

get parentId(): FormControl {
    return this.addForm.get("parentId") as FormControl;
}

这部分工作正常,控件已正确访问。

现在在我的ngOnInit中,如果执行此操作:
this.parentId.valueChanges.subscribe(() => console.log("Changed"));

控制台日志按预期在输入中更改的每个字符处执行。但是,如果我这样做:
this.parentId.valueChanges.pipe(tap(() => console.log("Changed")));

没发生什么事。没有错误,没有任何东西。我也尝试过使用map,switchMap等。似乎该管道无法在valueChanges上运行。我在代码的其他地方对不同的可观察对象使用了管道方法,没有任何问题。

而且我需要在此处使用管道,以进行反跳,映射等操作。

知道我在做什么错吗?

- 编辑 -

这是Angular Material网站上自动完成组件上的代码示例:
ngOnInit() {
   this.filteredOptions = this.myControl.valueChanges
       .pipe(
          startWith(''),
          map(val => this.filter(val))
       );
}

最后没有订阅,该示例有效。

最佳答案

您需要subscribe才能激活Observable

ngOnInit() {
   this.filteredOptions = this.myControl.valueChanges
       .pipe(
          startWith(''),
          map(val => this.filter(val))
       ).subscribe();
}

关于Angular 6-rxjs管道不适用于valueChanges,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50506896/

10-16 19:43