angular - 如何在 Angular 中对来自 api 的数据设置正面、负面和中性颜色和图标
问题描述
我想通过 API 设置我的数据,使其看起来像它在股票市场上的显示方式
正数变为绿色加图标,负数变为红色和向下图标,中性为带有向上和向下图标的黄色阴影。我尝试使用列表,但它不能正常工作尝试使用表仍然是相同的输出。我怎样才能做到这一点?
到目前为止我用一张桌子做了什么
<table class="table" id="htmlData">
<thead class="thead">
</thead>
<tbody class="list-group">
<tr class="list-group-item">
<td
style="vertical-align:bottom; text-align:left; font-size: 2em;font-weight: bold;color:white">
UMME
</td>
<td
style="vertical-align:bottom; text-align:left; font-size: 2em;font-weight: bold;color:white">{{report.umme | number : '1.2-2'}}
%
</td>
<td class="warning"
style="vertical-align:bottom; text-align:left; font-size: 2em;font-weight: bold;color:white"
[ngStyle]="{'background-color' : (report.umme_daily_change > 0) ? 'green': 'red' }">
{{report.umme_daily_change | number : '1.4-4'}}</td>
<td
style="vertical-align:bottom; text-align:left; font-size: 2em;font-weight: bold;color:white"
[ngStyle]="{'background-image' : (report.umme_daily_change > 0) ? getIncrease(): getDecrease() }"
style="background-repeat: no-repeat"></td>
</tr>
</tbody>
</table>
解决方案
您应该将所有三种颜色样式提取到 css 并在 css 类上使用条件
<td
[class.colorRed]="umme_daily_change < 0"
[class.colorNeutral] = "umme_daily_change === 0"
[class.colorPositive] = "umme_daily_change > 0
></td>
.colorRed{
background-color: red;
}
.colorNeutral{
background-color: yellow;
}
.colorPositive{
background-color: green;
}
使用该语法,在 html 元素上设置满足以下条件的类。
推荐阅读
- python - Pandas 将 Grouped-by 数据帧与每个组的另一个数据帧合并
- react-native - 如何使用特定目标运行 react-native run-ios
- python-3.x - 为什么是'amp;' 是否包含在我试图在phyton中使用BeautifulSoup抓取的链接('a')的许多部分中?删除它的更好方法是什么?
- mysql - 比较 2 个表并显示缺失的行
- android - Mapbox 地图不加载
- api - 我正在从 gfycat api 请求访问令牌。在下面的屏幕截图中我做错了什么?
- python - 从其他数据框中选择具有特定条件的行
- java - 如何将两个数组传递给一个可变参数
- docker - 如何使用 docker-compose v3 加入默认网桥和用户定义网桥?
- spring-boot - Spring cloud Config Vault 无法识别逗号分隔的应用程序名称