Rxjs - DistinctUntilChanged() with keyup event

前端 未结 1 1029
粉色の甜心
粉色の甜心 2021-01-27 13:18

I\'m using Rxjs 6 which filters an observable returned by Firebase based on (keyup) event in a form field.

I have an issue when the user keeps pressing backspace while t

相关标签:
1条回答
  • 2021-01-27 13:47

    The problem is that ALLWAYS make getData. You first look at changes, then switchmap to get Data. So you must have a Observable of changes. Use a "FormControl", not an input with [(ngModel)] So, in your .html

    <input type="text" [formControl]="search">
    

    The code must be

      search= new FormControl();       //Declare the formControl
    
      constructor() {}
    
      ngOnInit() {
        this.search.valueChanges.pipe(
             debounceTime(400),
             distinctUntilChanged(),
             tap((term)=>{
                  //here the value has changed
                  this.loadingIndicator = true;
             }),
             switchMap(filteredValue=> {
                  //We not return the value changed, 
                  return this.svc.getData().pipe(
                         map(_co => {
                             return _co.filter(...)
                         }),
                         tap(()=>{
                             this.loadingIndicator=false;
                         }))
              })).subscribe(result=>{
                 this.result=result
              })
      }
    
    0 讨论(0)
提交回复
热议问题