我有一个反应式表单,带有文本输入。为了便于在我的打字脚本中访问,我声明:
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,etc.nothing works。该管道似乎不能在valueChanges上工作。我在代码中的其他地方对不同的观察值使用了管道方法,没有任何问题。
我需要在这里使用管道,以便去反弹、映射等。
知道我做错了什么吗?
-编辑--
这是来自Angular Material site的代码示例,在Autocomplete组件上:
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(val => this.filter(val))
);
}
在最后没有订阅,示例可以正常工作。
发布于 2018-06-03 01:22:18
你需要subscribe
来激活Observable
,
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(val => this.filter(val))
).subscribe();
}
https://stackoverflow.com/questions/50506896
复制相似问题