你好,我在应用程序中有一个输入,它会在每个笔画上触发,但是我想要一些延迟,例如。 1秒后触发我的搜索功能,这样它就不会发送多个请求
我的HTML代码
<input id="musicsearch" (input)="onSearchChange($event.target.value)" ng-model-options="{debounce:1000}" class="form-control mr-sm-2"style="width: 100%;"type="text"placeholder="Search">
component.ts(处理更改)
onSearchChange(searchValue : string ) {
this.router.navigate(['/search', searchValue])
}
我是angular的新手,我找不到问题的解决方案,我想在用户停止输入时触发它
答案 0 :(得分:2)
尝试e = log(size)
size = 1 << e
base = p & ˜(size-1);
内的 setTimeout()函数,
onSearchChange()
答案 1 :(得分:0)
private modelChanged: Subject<string> = new Subject<string>();
private subscription: Subscription;
debounceTime = 500;
ngOnInit(): void {
this.subscription = this.modelChanged
.pipe(
debounceTime(this.debounceTime),
)
.subscribe(() => {
this.functionToBeCalled();
});
}
functionToBeCalled() {
console.log('Called After 500ms');
}
inputChanged() {
this.modelChanged.next("Akshay Is Awesome")
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
html文件
<input type="text" (keydown)="inputChanged()">
尝试此操作,稍后再说:)