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

100
Visualizações
Angular child event targets wrong parent component

I have some nested angular components that are giving me some problems with Event Emitters. The structure goes something like this:

<parent>
    <child-table id="table1">

    </child-table>
    <child-table id="table2">

    </child-table>
</parent>

Now the child-table component looks like this:

<toolbar (thingChanged)="onThingChanged($event)">
</toolbar>

and all onThingChanged() does is this:

@Output() thingChanged: EventEmitter<boolean> = new EventEmitter<boolean>(); //at global level
@Input() thingValue: boolean; //also global

onThingChanged(changed: boolean)
    {
        this.thingChanged.emit(changed);
        this.populateTable();
    }

and toolbar's html looks like this:

<mat-slide-toggle id="thingValue"
        [(ngModel)]='thingValue' (ngModelChange)='onThingChanged($event)'>
</mat-slide-toggle>

What I see when this app runs is two tables with mat-slide toggles on both. However, for some reason, if I click on the toggle for the table on the right (in this case the one with id=table2), it always changes toggles the slider for the table on the left, table1.

The behavior I expect is that when I click on the toggle for table2, I see table2 call it's own populateTable() method, not table1's.

If anyone has any suggestions as to why this might be happening, I'd appreciate your help.

EDIT: Here's a stackblitz that reproduces the problem. https://stackblitz.com/edit/angular-ivy-mhiihu?file=src%2Fapp%2Fapp.component.html

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

0

This is a bit of a guess, but if you have the exact same id on the toggle component could that be affecting it e.g. <mat-slide-toggle id="thingValue"

Maybe make this id dynamic

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