Angular 6将HTML中的背景色设置为CSS变量 [英] Angular 6 set Background Color in Html to CSS Variable

查看:361
本文介绍了Angular 6将HTML中的背景色设置为CSS变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular 6,并且我有一个简单的div,并且想要从模板内部设置此div的背景颜色.通过正常颜色时,此方法效果很好.但这不适用于CSS变量.

I am using Angular 6 and I have a simple div and want to set the background color of this div from inside the template. This works fine when passing normal colors. But this does not work with CSS Variables.

此示例有效

<div [style.background]="'red'">...</div>

此示例不起作用

<div [style.background]="'var(--some-css-var)'">...</div>

推荐答案

您必须使用ngStyle

You have to use ngStyle

<some-element [ngStyle]="{'background-color': styleExp}">...</some-element>

https://angular.io/api/common/NgStyle

这篇关于Angular 6将HTML中的背景色设置为CSS变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆