【发布时间】:2021-07-19 22:25:49
【问题描述】:
我从 JSON 文件而不是数据库中获取数据。我正在尝试根据 JSON 中的状态值为图标赋予颜色,例如 [如果绿色? class1:class2] 下面是我的代码。
我的 HTML 文件
<ng-container matColumnDef="status" [ngClass] = "{'color1': row.status ===
'GREEN', 'color2': row.status === 'RED'}">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Status</th>
<td mat-cell *matCellDef="let row" class="color1">{{row.status}}</td>
<td mat-cell *matCellDef="let row" class="color2">{{row.status}}</td>
</ng-container>
下面是我的 JSON 文件。
data:[
{
"name": "xyz",
"address": "post",
"status": "GREEN"
},
{
"name": "xyz1",
"address": "post1",
"status": "RED"
}
],
"count" : 2
}
这是我的 CSS
.color1{
color: green;
}
.color2{
color: red;
}
** 我无法更改状态图标的颜色。我得到了这个错误**
ERROR TypeError: Cannot read property 'status' of undefined
【问题讨论】:
-
看这里:angular.io/guide/….
-
什么是
row,它来自*ngFor?