如何在Angular 7中设置超时键

如何在Angular 7中设置超时键,angular,settimeout,delay,angular7,Angular,Settimeout,Delay,Angular7,我在谷歌上搜索过这个解决方案,但没有找到 已尝试1: <input type="text" #txtSearch (keyup)="onKeyUp(txtSearch.value)"> 尝试了2次 我在这里用的是类似的,但在Angular 7中不起作用 最后 我预计键控延迟为0.5s,然后是console.log(值)对于此类情况,您可以更好地使用rxJs中的debounceTime。甚至有更好的支持与角度。看看下面的例子- import { Component } from '@

我在谷歌上搜索过这个解决方案,但没有找到

已尝试1:

<input type="text" #txtSearch (keyup)="onKeyUp(txtSearch.value)">
尝试了2次

我在这里用的是类似的,但在Angular 7中不起作用

最后


我预计键控延迟为0.5s,然后是console.log(值)

对于此类情况,您可以更好地使用
rxJs
中的
debounceTime
。甚至有更好的支持与角度。看看下面的例子-

import { Component } from '@angular/core';
import { of, timer, Subject } from 'rxjs';
import { debounce, debounceTime } from 'rxjs/operators';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  model: string;
  modelChanged: Subject<string> = new Subject<string>();

    constructor() {
        this.modelChanged.pipe(
            debounceTime(500))
            .subscribe(model => {
              console.log(model);
            });
    }

    changed(text: string) {
        this.modelChanged.next(text);
    }
}

<input [ngModel]='model' (ngModelChange)='changed($event)' />
从'@angular/core'导入{Component};
从'rxjs'导入{of,timer,Subject};
从“rxjs/operators”导入{debounce,debounceTime};
@组成部分({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
模型:字符串;
模型更改:主题=新主题();
构造函数(){
此.model.pipe已更改(
去BounceTime(500))
.订阅(型号=>{
console.log(模型);
});
}
已更改(文本:字符串){
this.modelChanged.next(text);
}
}

应该有效。如果没有,请提供一份关于以下内容的报告:
import { Component } from '@angular/core';
import { of, timer, Subject } from 'rxjs';
import { debounce, debounceTime } from 'rxjs/operators';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  model: string;
  modelChanged: Subject<string> = new Subject<string>();

    constructor() {
        this.modelChanged.pipe(
            debounceTime(500))
            .subscribe(model => {
              console.log(model);
            });
    }

    changed(text: string) {
        this.modelChanged.next(text);
    }
}

<input [ngModel]='model' (ngModelChange)='changed($event)' />