I’m currently working on an Angular project and I’m trying to implement a feature where clicking a button triggers the deletion of a specific entity by passing its ID to a TypeScript method. However, I’m having trouble passing the ID from the TypeScript method to the HTML button to trigger the deletion event.
Here’s my current setup:
HTML:
<button mat-raised-button class="btn" color="warn" (click)="deleteStaffByID()">
Delete
TypeScript:
deleteStaffByID(id: any) {
console.log("Delete working!");
try {
this.staffService.deleteStaffDataByID(id).subscribe(data => {
this.dataSource = data;
alert("Staff deleted successfully!");
// this.listStaff();
console.log(data);
});
} catch(err) {
console.log("error in deleting the staff", err);
}
}
I’m unsure how to pass the ID from the HTML button to the deleteStaffByID() method in TypeScript. I’ve tried various approaches, including defining a different parameter in the TypeScript method, but none have been successful.
Could someone please guide me on how to properly pass the ID from the HTML button to the TypeScript method in this scenario? Any help would be greatly appreciated. Thank you!
2
Answers
This is how you generally do it with using
NgForOf
Note the
let-item
Try to pass id into deleteStaffByID() function as below:
Template Code: