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

101
Views
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 answers
Answer question

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 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!