【发布时间】:2020-10-11 19:34:00
【问题描述】:
我正在尝试以角度创建可自定义的标签组件,用户将在其中传递背景颜色,并且我希望根据传入的颜色将字体颜色自动设置为浅色/深色。
我的组件如下所示:
<span class="tag" [ngStyle]="{'backgroundColor': color}">{{ content }}</span>
ts 文件有:
@Input() color: string = '#ccc';
我找到了一些使用 sass 函数的教程:
@function set-notification-text-color($color) {
@if (lightness($color) > 50) {
@return #000000; // Lighter backgorund, return dark color
} @else {
@return #ffffff; // Darker background, return light color
}
}
.tag {
background: $notification-confirm;
color: set-notification-text-color($notification-confirm);
}
但它们似乎都在文件本身中定义了 background 属性,而我没有。有没有办法找出应用“标签”类的当前背景颜色并改用它?还是其他通用方式?
【问题讨论】:
-
如果将 {{content}} 包裹在另一个 中并将内部跨度的颜色属性设置为 'inherit' 会怎样?
标签: angular sass colors background-color