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

117
Visualizações
2 way filtering with AngularMaterial autocomplete form control

I want to create an Angular Material Autocomplete form control with two ways to filter the selectable options:

  1. The default way, when the user types in the text input, and only the options mathcing the search term will be visible, like in the examples on the Angular Material website.
  2. Above the autocomplete component there's a category select form control. When a category is chosen, I want to display only the elements in the chosen category, and allow the user to only search in these elements with the autocomplete text input.

Here is my code in the component class:

category = new FormControl('');
product = new FormControl('');
inwardForm = new FormGroup({
  category: this.category,
  product: this.product,
});


ngOnInit(): void {
    combineLatest([
      this.inwardForm.get('product')!.valueChanges,
      this.inwardForm.get('category')!.valueChanges,
    ]).pipe(
      startWith(['', undefined])
    ).subscribe(result=>{
      console.log(result);
    });
  }

My problem is that, after initializing the component, when I start typing in the autocomplete component, nothing happens. After chosing a category in the category select component, the console.log fires. After a category is chosen, and then typing to the autocomplete the console.log fires again at each keystroke.

So after a category is chosen, it works as expected, but I dont want the category select to be obligatory. What could be the problem?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Actually it doesn't matter which one you first touch. combineLatest fires only after the observables defined inside emits at least once. Currently neither is emitting from the beginning, so you need to interact with both of them to see the console log. I see you are trying to fire it up using startWith, but the startWith needs to be on the formcontrols instead of the combineLatest:

ngOnInit(): void {
  combineLatest([
    this.inwardForm.get('product')!.valueChanges.pipe(startWith('')),
    this.inwardForm.get('category')!.valueChanges.pipe(startWith('')),
  ])
  .subscribe((result) => {
    console.log(result);
  });
}

Though you could also just listen to the parent form, then would be no need for combineLatest:

this.inwardForm.valueChanges.subscribe(values => {
   console.log(values.product, values.category);
})

Remember to unsubscribe!

about 4 years ago · Juan Pablo Isaza 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