Angular 6 - rxjs 管道无法处理 valueChanges
Angular 6 - rxjs pipe not working on valueChanges
我有一个带有文本输入的反应式表单。为了便于在我的打字稿中访问,我声明:
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 的作品。管道似乎不适用于 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();
}
我有一个带有文本输入的反应式表单。为了便于在我的打字稿中访问,我声明:
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 的作品。管道似乎不适用于 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();
}