angularjs - 从 ngFor 循环中的元素覆盖“id” - AngularJS
问题描述
我正在尝试输出一个侧边栏菜单,该菜单通过包含来自 fonts.googleaplos.com 的图标名称的对象进行迭代 (*ngFor)。我希望图标以黑色背景显示为白色,当我单击它们时,将其反转(白色背景,黑色图标颜色)。单击图标时突出显示(背景变为白色)有效,但图标颜色不会改变。有没有办法单独更改所选的图标颜色?我试图更改所选图标的 id,但似乎无法覆盖 id="white-icon"。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-sidebar',
templateUrl: './sidebar.component.html',
styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent{
selectedIndex: number;
icons: any[] = [
{
"name": "home"
},
{
"name": "question_answer"
},
{
"name": "videocam"
},
{
"name": "description"
},
{
"name": "event"
},
{
"name": "check_circle"
}
];
public setRow(_index: number) {
this.selectedIndex = _index;
}
constructor() { }
ngOnInit(): void {
}
}
.mat-sidenav-container {
position: fixed;
height: 70%;
width: 64px;
}
.mat-sidenav{
background-color: rgb(87, 87, 146);
}
#white-icon {
color: white;
}
.highlight {
background-color: white;
color: black;
}
<head>
<link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined|Material+Icons+Two+Tone|Material+Icons+Round|Material+Icons+Sharp" rel="stylesheet">
</head>
<mat-sidenav-container>
<mat-sidenav mode="side" opened="true">
<!-- sidenav content -->
<mat-nav-list>
<div >
<a *ngFor="let icon of icons; let i = index;" (click)="setRow(i)" [ngClass]="{'highlight': selectedIndex === i}" mat-list-item >
<mat-icon mat-list-icon class="material-icons-round">{{ icon.name }}</mat-icon>
</a>
</div>
</mat-nav-list>
</mat-sidenav>
解决方案
推荐阅读
- angular - Angular CDK 拖放 - 对齐网格
- python - 如何根据前一个值和下一个值向 Pandas 数据框插入行?
- android - 获取谷歌地图准确位置
- android - Gradle AndroidStudio上生成signedbundle时如何重命名实现app-release.aab
- google-cloud-storage - GCS:如何使用最低权限服务帐户备份和保留版本
- javascript - #heremaps 如何在javascript中单击集群时获取信息气泡中的数据
- vba - 对于每个 +if (or) 语句
- networking - 可以通过IPV6实现端口转发的功能吗?
- javascript - JavaScript:找到最大间隔重叠的点
- php - SQL中where子句的两个条件