24

Hello I have an input in my application which fires on every stroke but I want some delay so for eg. trigger my search function after 1 sec so that it doesn't send multiple requests

My Html Code

<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 (which handles change)

 onSearchChange(searchValue : string ) {    
    this.router.navigate(['/search', searchValue]);      
  }

I am new to angular I can't find solution for my problem, I want to trigger this when user stop typing in input

1
  • maybe use (blur) instead of (input)? Commented Jul 22, 2020 at 19:42

3 Answers 3

36
  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 file <input type="text" (keydown)="inputChanged()">

Try This Thanks me later :)

Sign up to request clarification or add additional context in comments.

7 Comments

@Akashay Cannot find name 'Subject'. , Cannot find name 'Subscription'. and Cannot find name 'debounceTime'. Did you mean the instance member 'this.debounceTime'?
@SunilMeena,You need to import those.. If you look at the link i provided in my answer comment you will come to know..
import { Subject } from 'rxjs/Subject'; import { Subscription } from 'rxjs/Subscription'; import { debounceTime, distinctUntilChanged } from 'rxjs/operators'; Hope This helps :)
I've found if you use the (keyup) event you'll get all characters. (keydown) was leaving off the last character for me.
Hi @Nate the event has been added just for reference purpose, we can use change, input, keydown or another valid event :)
|
9

On Html file

<input [(ngModel)]="searchTxt" type="text" (keyup)="triggerSearch()">

On ts file

searchTxt:string = '';
timeout = null;

triggerSearch() {
    clearTimeout(this.timeout);
    this.timeout = setTimeout(() => {
      this.onSearchChange(this.searchTxt);
    }, 1000);
  }

Comments

0

Just came across this issue and tried to find an improved solution. Came up with the following using the ReactiveForms capabilities and RxJs (debounceTime).

Bind the input with the formControl attribute.

<input [formControl]="control">

Put it together in the typescript component.

control = new FormControl();

constructor() {
  this.control.valueChanges
    .pipe(debounceTime(500))
    .subscribe((value: string) => {
      // do something with the value
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.