angular - *ngFor 内的 Angular [ngClass] 用于选定项目
问题描述
嗨,我需要一些帮助来将 [ngClass] 设置为单击的项目。我在css文件中有课。即使组件被销毁,我也需要这种样式保留在选定的项目上。我所拥有的不起作用。scc 不适用于单击的项目。
<ul *ngFor="let item of list$ | async" [ngClass]="{ selected: selectedSong === item }">
<li><h5><a (click)="sentItem(item.ID)">{{ item.name }}</a></h5></li>
<li>{{ item.description }}</li>
</ul>
selecedSong:any;
sentItem(ID: string) {
this.musicService.setViewed(ID);
this.selectedSong = ID;
this.router.navigate(["/some-route",ID]);
}
解决方案
你能试试这个吗?在您的 html 文件中,使用双等号 (==) 而不是 === 替换条件,以防万一值与类型不匹配。
[ngClass]="{ 'selected': selectedSong == item.ID }">
推荐阅读
- c# - XML 键值对 C#
- c - 标准 C 是否接受 `{0}` 作为任何结构的初始值设定项?
- influxdb - 我可以有 2 个独立的 InfluxDB 安装吗?
- javascript - 将对象转换为带有对象的数组
- docker - 如何在 Docker Compose 中从服务器设置环境变量?
- zapier - 将字符串添加到 Zapier 中的空列表
- objective-c - Objective-c等价于java匿名接口实现
- java - 继承 java-bank 账户 (java)
- json - 将 JSON 字段转换为 ReasonML 变体
- c++ - 从类成员函数构造 std::thread