去抖动以获取angular2中的值输入

去抖动以获取angular2中的值输入,angular,Angular,我想在angular 2中的几次(许多毫秒或几秒钟)后从输入文本中获取一个值,当一个自定义用户编写输入时,不必等待他单击按钮 我已经试过了,但即使我使用了debounceTime,每次按键都会发送值 我试图了解debounce和observable,这就是我的理解,有人能帮我修复代码吗: component.html: {{card.title} 组件。ts newTitle:string; 模式更改:主题=新主题(); 构造函数() 这个模式改变了 .debounceTime(500)//在

我想在angular 2中的几次(许多毫秒或几秒钟)后从输入文本中获取一个值,当一个自定义用户编写输入时,不必等待他单击按钮

我已经试过了,但即使我使用了
debounceTime
,每次按键都会发送值

我试图了解debounce和observable,这就是我的理解,有人能帮我修复代码吗:

component.html:

{{card.title}
组件。ts

newTitle:string;
模式更改:主题=新主题();
构造函数()
这个模式改变了
.debounceTime(500)//在发出最后一个事件之前
.distinctUntilChanged()
.subscribe(model=>this.newTitle=model);
}
重命名():void{
this.renameRequest.emit(this.newTitle);
}

好吧,有很多方法可以实现这一点,但这里有一种方法:

<input *ngIf="edit" type="text" #input="ngModel" [(ngModel)]="card.title" (ngModelChange)='rename()'/>
这是你的电话号码

您甚至可以订阅modelChange,如下所示:

ngAfterViewInit(){
       this.input.update // this is (modelChange)
             .pipe(debounceTime(500)) before emitting last event
             .pipe(distinctUntilChanged()) 
             .subscribe(model => (value)=>{
                   console.log('delayed key press value',value);
              });

}

请参考这个答案是的,值保存在@ViewChild('input')input中,但从未调用函数rename(),您知道吗?我遇到了这个错误:TypeError:无法读取未定义的属性'valueChanges'。当我添加条件(if(this.input))时,它可以工作,但每个按键的值都在变化。我只需在html代码中删除(ngModelChange)=“rename()”,它工作得很好,谢谢@MiladOn最近的角度版本,您需要
pipe()
它,所以它看起来像:
this.input.valueChanges.pipe(debounceTime(500),distinctUntilChanged())。订阅
它为
重命名()提供
未定义的
。我从HTML中删除了
ngModelChange
,也从
subscribe
中删除了
model
,然后它就工作了。
ngAfterViewInit(){
       this.input.update // this is (modelChange)
             .pipe(debounceTime(500)) before emitting last event
             .pipe(distinctUntilChanged()) 
             .subscribe(model => (value)=>{
                   console.log('delayed key press value',value);
              });

}