简体   繁体   English

如何从 Angular 的模板中引用我的 object?

[英]How to refer to my object from template in Angular?

I have project with array of notifications.我有一系列通知的项目。 I add them to template using *ngFor.我使用 *ngFor 将它们添加到模板中。 How can I pass some data about current notification from template to delete it from array?如何从模板传递有关当前通知的一些数据以将其从数组中删除?

My project.ts我的项目.ts

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

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

My project.html我的项目.html

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

In your component.ts在你的 component.ts

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM