【发布时间】:2018-06-02 13:46:47
【问题描述】:
我正在构建一个自动完成功能,该功能正在查询后端以获取建议,并且希望仅在用户输入 Angular 5 表单控件时获得特定延迟的最后一个查询。目前我的代码看起来像
this.newVendorForm.get('address').valueChanges.pipe(delay(3000)).subscribe(
address => {
this.geocodeApi.getAddressSuggestions(address)
.subscribe(
response => {
console.log('address suggestions');
console.log(response);
this.addressSuggestions = response;
},
error => {
console.log('error getting address suggestion');
console.log(error);
}
)
}
);
这可行,但是它会在 3000 毫秒后对每个输入的字母进行查询。例如,'test' 将在 3000 毫秒后查询 ['t', 'te', 'tes', 'test']。如何在 3000 毫秒延迟后从 valueChanges 中获取最后一个更改(即“测试”),然后进行订阅?谢谢你的帮助
【问题讨论】:
标签: angular typescript autocomplete rxjs angular-observable