首页 > 解决方案 > 如何从 Angular 的模板中引用我的对象?

问题描述

我有一系列通知的项目。我使用 *ngFor 将它们添加到模板中。如何从模板传递有关当前通知的一些数据以将其从数组中删除?

我的项目.ts

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

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

我的项目.html

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

标签: javascriptarraysangular

解决方案


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

在你的 component.ts

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

推荐阅读