javascript - 如何从 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>
解决方案
<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);
}
推荐阅读
- php - 如何将 XML 发送到 wsdl 端点
- python - 在 anaconda windows 中从 Python 调用 c++ 函数
- python - 消费者无法连接到 amqp || 芹菜
- java - 如何使用 Hibernate 从存储过程中检索输出参数
- http - 无法使用 etaoin 启动 chrome 或 Firefox 驱动程序
- java - 命令提示符中的 Unicode 字符
- google-cloud-platform - 如何知道是否将零字节视频上传到 gcp 存储桶?
- c# - 编写正确的查询
- amazon-web-services - 如果只有几次尖峰,应该为 dynamodb 设置 minRCU 和 minWCU 什么?
- javascript - 重定向到新的子域并在重定向的子域上设置 cookie