I want to get a value from an input text after a few time (many millisecond or seconds) in angular 2, when a custom write an input but without waiting him to click a button.
I have tried this, but even when I use debounceTime
, value is send in every keypress.
I try to learn about debounce and observable and this is what I understand, Can anyone please help me to fix my code:
component.html:
<md-card-title *ngIf="!edit">{{card.title}}</md-card-title>
<input *ngIf="edit" type="text" [(ngModel)]="card.title" (ngModelChange)='rename()'/>
component.ts
newTitle: string;
modelChanged: Subject < string > = new Subject < string > ();
constructor()
this.modelChanged
.debounceTime(500) //before emitting last event
.distinctUntilChanged()
.subscribe(model => this.newTitle = model);
}
rename(): void {
this.renameRequest.emit(this.newTitle);
}