Yurii
Yurii

Reputation: 95

How to refer to my object from template in Angular?

I have project with array of notifications. I add them to template using *ngFor. How can I pass some data about current notification from template to delete it from array?

My project.ts

  public notifications: Notification[] = [...someData];

  clearOne() {
    ...should delete from array
  }

My project.html

<div *ngFor="let n of notifications">
  <div (click)="clearOne">{{ n }}</div>
</div>

Upvotes: 0

Views: 74

Answers (1)

Kirubel
Kirubel

Reputation: 1627

<div *ngFor="let n of notifications">
  <div (click)="clearOne(n)">{{ n }}</div>
</div>

In your component.ts

clearOne(notification) {
  // Remove from array
  let index = this.notifications.indexOf(notification);
  this.notifications.splice(index, 1);
}

Upvotes: 2

Related Questions