在我的angular应用程序下
我正在使用此代码设置自定义正文样式:
constructor(private elementRef: ElementRef) { }
ngOnInit() {
this.elementRef.nativeElement.ownerDocument.body.style.overflowY = 'hidden';
}
对于一些特殊的情况,我会在上面加上“重要”
这将是这样的:
this.elementRef.nativeElement.ownerDocument.body.style.overflowY = 'hidden !important';
但这是行不通的,“重要的不是添加到风格。
注意; 因为我需要将此应用于主体本身,而不是应用于组件中的特定html元素,所以ngStyle无法做到这一点。
建议??
试试这个。 为什么使用ElementRefs? 只需使用[ngStyle]
<div [ngStyle]="{ 'overflowY' : 'hidden !important' }"></div>
或
<div [style.overflowY ]="'hidden !important'"></div>
另外,如果它在加载时自动发生(在ngoninit
),为什么不简单地在模板中对它进行硬编码呢? 为什么你不简单地使用CSS并添加Angular呢?