Angular:如何以编程方式将 css 样式设置为正文,与打字稿中一样重要

fir*_*baa 4 javascript css typescript angular-template angular

在我的角度应用程序下

我使用此代码来设置自定义正文样式:

  constructor(private elementRef: ElementRef) { }

  ngOnInit() {
    this.elementRef.nativeElement.ownerDocument.body.style.overflowY = 'hidden';
  }
Run Code Online (Sandbox Code Playgroud)

对于某些特定的场景我必须添加“重要”

这会是这样的:

   this.elementRef.nativeElement.ownerDocument.body.style.overflowY = 'hidden !important';
Run Code Online (Sandbox Code Playgroud)

但这是行不通的,并且“重要”没有添加到样式中。

要注意 ; 因为我需要将其应用于主体本身,而不是应用于组件之外的特定 html 元素,所以 ngStyle 无法做到这一点。

建议??

Jer*_*Mah 5

应该有效。

this.elementRef.nativeElement.ownerDocument.body.style.setProperty("overflow-y", "hidden", "important");
Run Code Online (Sandbox Code Playgroud)