Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda