【发布时间】:2021-04-09 14:59:44
【问题描述】:
我有一个 Angular 组件,它在运行时动态生成 mat-checkbox,我需要用不同的颜色以不同的方式更改每个复选框的各个背景,而且我没有(不会)事先有信息,只能在运行时。
我有以下ng-template 复选框:
<ng-template #renderCheckbox let-id="id" let-attr="attr">
<mat-checkbox
[checked]="attr.show"
[color]="'custom-' + id"
(change)="onChange($event.checked, attr)">
{{attr.name}}
</mat-checkbox>
</ng-template>
其中,模板中的attr 具有以下接口类型,这些信息是从Highcharts' 系列中提取的,我不想硬编码color。
interface LinkedSeriesAttributes {
id: string;
name: string;
index: number;
color: string;
checked: boolean;
}
由于无法预先创建 css 类,也无法直接将颜色应用于 mat-checkbox,因此我只能在模板开头生成 <style>...</style>。
在我的组件中,我的代码会生成这样的样式:
.mat-checkbox.mat-custom-hello.mat-checkbox-checked .mat-checkbox-background::before {
color: #6E8BC3 !important;
}
.mat-checkbox.mat-custom-world.mat-checkbox-checked .mat-checkbox-background::before {
color: #9ED6F2 !important;
}
...
但是,我尝试了各种方法将其转储到 <style> 中,但均未成功。我试过了:
<style>{{ dynamicCSSStyles }}</style>
其中,我的 IDE 显示花括号有错误,虽然它编译正常并且运行没有错误,但我什么也没得到,甚至看不到 <style> 标记。
我还尝试将<style> 包含在我的dynamicCSSStyles 变量中,而angular 只是将整个内容作为文本转储...
在 Angular 中生成<style> 的正确方法是什么。
【问题讨论】:
标签: html css angular highcharts angular-material