Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Search data in Observable

I'd like to have a search input, that display the result on keypress. At the moment, this is what I have :

mylist: Observable<MyData[]>;
term = new FormControl();
    ngOnInit() {
        this.mylist = this.term.valueChanges
                    .debounceTime(400)
                    .distinctUntilChanged()
                    .switchMap(term => this.searchData(term));
    }

    searchData(valueToSearch:string){
        if(valueToSearch == ''){
            this.channels = MyData.find();
        }
        return MyData.find({'title':new RegExp(valueToSearch)});
    }

It works quite well, but I have trouble to initialize "mylist", and I think my method isn't performant at all.

Basically, I want when my component is initialize, that:

this.mylist = MyData.find();

And on keypress, I want my search to be done on this.mylist, to avoid doing too much request. Is it possible ? I hope I'm clear.

Thanks by advance guys.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You must subscribe to the mapped data. Modify to the below code

this.term.valueChanges
    .debounceTime(400)
    .distinctUntilChanged()
    .switchMap(term => this.searchData(term))
    .subscribe((result) => {
            this.mylist = result
        });;

@Julia is correct , modify your searchData() function as below

searchData(valueToSearch:string):Observable<any> {
        if(valueToSearch == ''){
            this.channels = MyData.find();
        }
        return <Observable<any>>MyData.find({'title':new RegExp(valueToSearch)});
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!