【发布时间】:2018-05-15 11:55:42
【问题描述】:
我有以下情况: 我创建了一个使用第 3 方组件的组件,该组件然后使用 JS 库(用于图表),然后生成我需要从我的组件设置样式的 HTML/SVG。
所以我们有类似的东西
(MyComponent) => (3rdPartyComponent) => (JSLibrary) => 动态生成的 HTML/SVG
生成的 SVG 看起来像这样:
<svg><g class="labels">...</g></svg>
因为单个页面上可以有多个图表,所以 MyComponent 可能会被多次实例化,并且每个生成的 SVG 的样式可能不同。
我现在想根据一些 @Input 到 MyComponent 以编程方式更改图表标签的字体大小,例如useSmallLables.
我该怎么做?
更多细节。 这是我想要的(不工作的)伪代码:
@Component({
selector: 'app-my-component',
template: '<third-party [ngClass]="getCssClasses()" ...></third-party>",
style: ''
})
export class MyComponent ... {
@Input() useSmallLabels = false;
// we need a unique class name to be able to apply styles to a specific SVG, not all
uniqueCssClass = generateSomeUniqueId();
getCssClasses() {
const cssOptions = {};
// add the unique class
cssOptions[this.uniqueCssClass] = true;
// This it what I would like to do, but of course does not work with ngClass or anything else as far I know.
// We would need ::ng-deep as the generated SVG does not know about angular emulated shadow-dom attributes and vice-versa
const css = '::ng-deep .' + this.uniqueCssClass + ' .labels { font-size: 0.5rem; }';
cssOptions[css] = this.useSmallLabels;
return cssOptions;
}
我深入挖掘以找到解决此问题的方法,但我从未找到解决方案。使用老式的 JQuery 和其他东西,这可能根本不是问题。
【问题讨论】:
标签: angular typescript sass