Salma
Salma

Reputation: 217

debounce to get a value input in angular2

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);
}

Upvotes: 16

Views: 32611

Answers (2)

Vlad Lego
Vlad Lego

Reputation: 1698

Angular 12.2 + RxJS + lodash

If you're using lodash. $ npm i --save lodash

<input (keyup)="debouncedSearch($event)"/> 
import { debounce } from 'lodash';

...

  debouncedSearch = debounce(
        (event) => this.search(event.target.value),
        400
  )

  search(query: string){
    console.log(query)
  }

...
  <input (keyup)="debouncedSearch()" [(ngModel)]="theInput" /> 

Version with local model

If you have a local var to bind to, you can also do this:

It's more readable and you can set your var in ngOnInit or wherever you need to.

import { debounce } from 'lodash';

...

  theInput: string = ''

  debouncedSearch = debounce(this.search, 400)

  search(){
    console.log(this.theInput)
  }

...

Upvotes: 0

Milad
Milad

Reputation: 28592

Well, there is lot's of ways to achieve that, but here is one way :

<input *ngIf="edit" type="text" #input="ngModel" [(ngModel)]="card.title" (ngModelChange)='rename()'/>

And inside your class

newTitle : string;
@ViewChild('input') input;
constructor()

}

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

}

rename(value): void {
    this.renameRequest.emit(value);
}

Here is the Plunker

You can even subscribe to modelChange like bellow :

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);
              });

}

Upvotes: 13

Related Questions