0

I am using a simple input in angular.

           <ng-template #searchinput>
              <mat-form-field class="w-100" floatLabel="never">
                <input matInput placeholder="Search" [id]="'search_'+col.columnDef" (input)="searchChange($event.target.value, col.columnDef)">
              </mat-form-field>
            </ng-template>

i want to add debounceTime in the event.

searchChange(event, colName) {
    console.log(event, colName);
    event.pipe(
      debounceTime(300), 
      distinctUntilChanged())
      .subscribe(value => console.log(value)
    );    
  }

But this is not working ERROR TypeError: event.pipe is not a function. Need some help.

3
  • What does the console say? What is the error? Commented Oct 23, 2020 at 5:14
  • added the error, event is basically the text not observable Commented Oct 23, 2020 at 5:54
  • Approving the right answer would be appreciated :) Commented Oct 27, 2020 at 13:47

2 Answers 2

1

Your event is actually the value from your input field. If you want to add debounceTime, you have to do the following;

private value$ = new Subject<string>();

ngOnInit() {
  this.input$
    .pipe(
      debounceTime(300),
      distinctUntilChanged()
    )
    .subscribe((input: string) => {
      console.log(input);
    }
}

searchChange(input, colName) {
  this.input$.next(input);
}
Sign up to request clarification or add additional context in comments.

Comments

1

The correct answer is the Marek W's one. Another aproach is using a FormControl, and subcribe to valueChanges just

control=new FormControl();
ngOnInit()
{
 this.control.valueChanges.pipe(
      debounceTime(300),
      distinctUntilChanged()
    )
    .subscribe((input: string) => {
      console.log(input);
    }
}

And

<input [formControl]="control">

1 Comment

Or use @viewChild on the input and then fromEvent() to create a stream directly from the DOM event. FormControl gives you a lot of extras though, so I like this answer best. I also think binding a FormControl with the directive looks cleaner and is easier to understand.

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.