【问题标题】:Angular Material2 - Programmatically setting colorAngular Material2 - 以编程方式设置颜色
【发布时间】:2018-02-15 14:11:53
【问题描述】:
我正在寻找一种方法来在我的组件内运行时更改主题中的“重音”颜色。例如在构造函数中。
我一直在寻找这个没有运气。我找到的所有答案都告诉我如何预定义一个主题文档,该文档将定义“primary”、“accent”、“warn”的颜色。
我的应用程序在加载时通过 AJAX 获取颜色(例如“#FFFFFF”),并且需要在运行时将它们应用于“主要”或“重音”。
这可能吗?
【问题讨论】:
标签:
css
angular
material-design
【解决方案1】:
您可以做些什么来添加 css 使用 viewchild 操作您的容器并添加内部 css。
@ViewChild ('yourcontainer') container: ElementRef;
private yourColorGetted: string = '#FFFFFF';
private yourBackgroundColorGetted: string = '#FFFFFF';
this.container.nativeElement.insertAdjacentHTML ('beforeend', `<style>
.candy-carousel {
background-color: ${this.yourBackgroundColorGetted},
color: ${this.yourColorGetted}
}
</style> `);
<div id="yourcontainer"> </ div>