随着CSS 4中自定义属性的引入,我想将它们绑定到Angular中。
通常,将使用如下自定义属性:
<div style="--custom:red;">
<div style="background-color: var(--custom);">
hello
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但是,当使用Angular的绑定机制时,它不会导致红色矩形:
<div [style.--custom]="'red'">
<div style="background-color: var(--custom);">
hello
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
那么如何绑定自定义属性?
我认为您目前无法那么容易地绑定,但是有一个解决方法。由于自定义属性受益于级联,您可以在组件中设置属性并在组件内部的其他地方使用它。
constructor(
private elementRef: ElementRef
) { }
ngOnInit() {
this.elementRef.nativeElement.style.setProperty('--custom', 'red');
}
Run Code Online (Sandbox Code Playgroud)
我从这里获取它https://github.com/angular/angular/issues/9343#issuecomment-312035896,但渲染器解决方案对我不起作用
| 归档时间: |
|
| 查看次数: |
619 次 |
| 最近记录: |