angular - Angular:以编程方式设置复杂 CSS 选择器的样式
问题描述
我正在寻找一种以编程方式执行以下操作的方法:
tr.product-row:not(.expanded-row):active {
background-color: red; <--this part will be calculated in my .ts file
}
原因是我希望活动颜色是不同元素颜色的较浅版本。我可以计算出我需要的颜色,但不知道如何设置。
可以用 Renderer2 完成吗?
解决方案
使用可以在模板中绑定的 CSS 变量怎么样:
CSS:
tr.product-row:not(.expanded-row):active {
background-color: var(--some-var); // specify some variable
}
模板:
<!-- bind to that variable using directive against component property -->
<div [style.--some-var]="theVariableInComponentFile">foobar</div>
这是一个绑定到 CSS 变量的示例。
希望这会有所帮助!
推荐阅读
- php - 如何使用 psysh php 执行系统命令?
- c# - 如何重载泛型 IEnumerable
- numpy - 如何修复 ValueError:具有形状 (2,1) 的不可广播输出操作数与广播形状 (2,2) 错误不匹配?
- javascript - 如何在浏览器上运行从 Github 下载的 Angular nodejs 和 MongoDB crud 项目?
- python - Is it sure to authenticate with GitHub using requests with my username and a token generate in GitHub?
- algorithm - 是否有将多边形转换为正方形图像的算法?
- node.js - 从 Azure IoT Edge 设备直接调用方法到没有 Internet 连接的子设备
- python - 替代嵌套 np.where 语句以保留 NaN 值,同时基于其他两个现有列创建新的 pandas 布尔列
- node.js - Nodejs 未连接到在 Sandbox 中运行的 MongoDB
- python-3.x - Pandas:运行外部库函数以有效地创建新列