我在Ionic 5中尝试了这个,它有效:
<ion-content style="--background: yellow;">
然而,我想普通地改变颜色。我尝试过,但没有成功:
<ion-content [ngStyle]="{'--background': backgroundColor}">
我找不到错误。
NgStyle
不支持css变量
这是一个"已知的";限制,并且已经存在了很长一段时间:
https://github.com/angular/angular/issues/9343
然而,即使有些丑陋,也有变通办法。我更喜欢这个:
<ion-content [attr.style]="sanitizer.bypassSecurityTrustStyle('--background: ' + backgroundColor)">
// you will need a DomSanitizer instance in your component
constructor(private sanitizer: DomSanitizer) {}