angular - Ionic - custom colors from SASS file in ngStyle
问题描述
In my Ionic project, in my src/theme/variables.scss
, below the $colors
group, I have another group of colors $buttonColors
which looks like this:
$colors: (
primary: #488aff,
secondary: #32db64,
danger: #f53d3d,
light: #f4f4f4,
dark: #222
);
$buttonColors: (
sampleRed: #A92A0E,
sampleBlue: #162C60
);
In my .css file I can use the second group of colors like this:
.element {
color: color($buttonColors, sampleRed, base);
}
But if I want to use the code from above in [ngStyle]="{'color' : 'color($buttonColors, sampleRed, base)'}"
- this would not work.
And in my case I want to use this color specifically in ngStyle. How can I reference to this color correctly in ngStyle
?
解决方案
在组件文件中:
import {Sanitizer} from '@angular/core';
export class MyCass {
sanitizer: Sanitizer;
myColor = '#444444';
constructor(_sanitizer: Sanitizer) {
this.sanitizer = _sanitizer;
}
在 HTML 文件中:
<ion-title [attr.style]="sanitizer.bypassSecurityTrustStyle('--color: ' + myColor)"></ion-title>
它对我有用。但是你必须使用 ionic 4 CSS 变量。
推荐阅读
- c# - 使用 OleDB 从 Access DB 中删除问题
- javascript - 淡入淡出效果只执行一次
- jquery - 数据表实时加载每页记录
- android - 在 webview 中显示 PDF 文件(已发布的 google 电子表格)
- javascript - 如何在本机反应中从firebase firestore获取除一份文件外的所有文件?
- laravel - Laravel:获取多个用户同时共享的多对多关系
- unity3d - 如何:获取对 UI 图像的引用
- version-control - 从 Aldon 迁移到另一个版本控制产品
- mysql - 通配符字符串选择 MySQL
- javascript - 如何将 jQuery 代码翻译成 Vanilla JS