html - Angular HTML - 基于布尔值更改表格单元格背景颜色
问题描述
尝试使用 Angular ng-class 设置表格单元格背景颜色。我正在关注这些帖子的片段:
角 9.1.3
材料~9.1.3
我已将样式放入 app.component.scss 文件中:
.routeRed {
background-color: red;
}
.routeGreen {
background-color: green;
}
我正在使用标准 Angular 加载 app.component.scss 文件:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
我对 app.component.html 进行了如下编码: <table> 确实位于 Angular Material mat-tab 中,但我不认为这会阻止任何 scss 样式,至少我没有找到任何说材料可以防止这种情况。
<table border="1" class="table" id="idTableCamelRoutes" matSort
(matSortChange)="sortRoutes($event)" style="width: 100%">
<thead>
<tr>
<th mat-sort-header="routeId">Route Id</th>
...Removed for brevity...
</tr>
</thead>
<tbody>
<tr *ngFor="let item of applicationState.camelRoutes">
<td>{{ item.routeId }}</td>
<td>{{ item.routeStatus }}</td>
<td ng-class="{'routeRed' : !(item.routeRunning), 'routeGreen': item.routeRunning}">{{item.routeRunning}}</td>
<td>
ngFor 只是遍历 CamelRouteBean 对象的集合:
export class CamelRouteBean {
routeId: string;
routeStatus: string;
routeRunning?: boolean;
description?: string;
}
所以,我所要做的就是对 ng 类使用 item.routeRunning (一个布尔值)......我还验证了 item.routeRunning 被设置为 true 或 false 就好了。我验证了 app.component.scss 也正在加载。但无论我尝试什么,表格单元格的背景颜色都不会从白色改变。当我在 Chrome 中检查元素时,我没有看到任何样式都应用于表格单元格。
我错过了什么?
蒂亚阿迪姆
解决方案
我想你可以像下面这样:
<div
[ngClass]="{
'routeGreen': item.routeRunning,
'routeRed': !item.routeRunning
}"
></div>
推荐阅读
- mongodb - MongoDB 替换所有匹配的文档
- mongodb - 数组子文档中的某些值需要是唯一的
- matlab - 在具有给定协方差矩阵的二维正态分布上生成数字
- spring - 为什么测试类没有使用 spring boot 2.5.5 设置 ActiveProfiles?
- agda - 为什么 Agda 在这里需要模式匹配
- ios - AVVideoCompositionCoreAnimationTool 在使用 AVAssetExportSession 导出期间没有为自定义属性设置动画
- arrays - 更改 Mui-datatable 的数据时收到错误:对象作为 React 子项无效(找到:带有键 {} 的对象)。如果你打算渲染一个
- github - 我可以使用 Github Actions 在同一组织的仓库中更新另一个提交的仓库吗?或者在 GH Pages 中显示内容而不提交它们?
- docker - 在远程 Docker 容器中开发时使用本地 Clang 格式
- git - Git:在以前的提交中删除旧模式 100644 新模式 100755