Last active
April 20, 2021 18:20
-
-
Save harbirchahal/573f74dc93abeef761af79b3ee060c0f to your computer and use it in GitHub Desktop.
Event Bubbling in Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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