Skip to content

Instantly share code, notes, and snippets.

@harbirchahal
Last active April 20, 2021 18:20
Show Gist options
  • Save harbirchahal/573f74dc93abeef761af79b3ee060c0f to your computer and use it in GitHub Desktop.
Save harbirchahal/573f74dc93abeef761af79b3ee060c0f to your computer and use it in GitHub Desktop.
Event Bubbling in Angular
<div (click)="handleAction($event)">
<div *ngFor="let item of listing">
<div>{{ item.label }}</div>
<div>
<button [attr.data-id]="item.id"
data-action="modify">
Modify
</button>
<button [attr.data-id]="item.id"
data-action="delete">
Delete
</button>
</div>
</div>
</div>
import { Component, Input, Output, EventEmitter } from '@angular/core';
interface Item {
id: string;
label: string;
}
@Component({
selector: 'event-bubble',
templateUrl: './event-bubble.component.html'
})
export class EventBubbleComponent {
@Input() listing: Item[] = [];
@Output() modify = new EventEmitter<string>();
@Output() delete = new EventEmitter<string>();
handleAction(event) {
const {id, action} = event.target.dataset;
if (action === 'modify') {
this.modify.emit(id);
} else if (action === 'delete') {
this.delete.emit(id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment