【发布时间】:2019-05-08 17:30:15
【问题描述】:
我有一个使用*ngFor 生成的项目列表:
<div *ngFor="let item of items">
<div>
<span>item.description<span>
<span>item.price</span>
<button (click)="removeItem(item.id)"> x <button>
</div>
...
removeItem 函数向 API 端点发出 DELETE 请求并成功删除该项目,但除非我刷新页面,否则该项目仍保留在视图中。
我想要的是应用一个类来设置display:none到被删除的项目,我用过这个:
<div *ngFor="let item of items" [class.dismissed]="itemRemoved">
在.ts 文件中,itemRemoved 是这样初始化的:
itemRemoved: boolean = false;
并且在函数中removeItem在API调用成功时设置为true:
this.http.delete(url)
.subscribe(
response => {
console.log("Item removed");
this.itemRemoved = true;
},
但该类适用于所有项目。
如何将类仅应用于我要移除的项目?
感谢您的帮助!
【问题讨论】:
-
如果该项目从
items数组中删除,它将从 DOM 中删除。应该没有必要使用display: none -
如@user184994 所述,您可以修改
removeItem方法以使用传递的id 搜索item,然后从@ 中删除它(使用splice或任何其他方法) 987654338@数组 -
@roymckrank 你能显示你的组件代码以便我可以帮助你