FormControl valueChanges
Observable的工作方式与其应有的方式相同。在我的例子中,除了当前值之外,我需要停止为输入字段中以前的所有更改触发valueChanges
。我一直在尝试使用下面的代码来做到这一点-
private getAutocompleteSuggestions(input){
return this.SS.searchProducts(parseInt(this.filter), '', input);
}
private searchSuggestionsAPIRequest(input){
this.queryField.valueChanges
.debounceTime(1000)
.distinctUntilChanged()
.switchMap((input) => this.getAutocompleteSuggestions(input))
.subscribe( res => {
// The res is triggered for all value changes in
// queryField formcontrol
// Tried with following solution to stop emit previous
// changes but it does not work
this.queryField.setValue(input, { emitEvent: false });
});
}
我一直在寻找所有可能的方法来停止释放以前的值,但得到了这个解决方案-
this.queryField.setValue(input, { emitEvent: false });
我想,我遗漏了一些东西。如果有任何进一步的解决方案或文档,那就太好了。
提前谢谢。
发布于 2019-07-02 13:05:58
ValueChange是一个观察值,每次你输入的时候,它都会触发发射。您可以在switch map中调用您的api,并使用来自输入的最新值将其结果订阅到那里,并保持valueChanges行为不变。
searchBook() {
this.bookId.valueChanges.pipe(
debounceTime(1000),
distinctUntilChanged(),
switchMap(input => {
this.getAutocompleteSuggestions(input)).subscribe(res =>{
//DO you required processing
})
})
).subscribe(model => model);
}
https://stackoverflow.com/questions/56845106
复制相似问题