Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

99
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda