在运行时将样式应用于 Angular 中的 NgBootstrap 模式



我有一个应用程序,可以在运行时交换主题。

BaseComponent 是我的主要组件,它添加到我的 AppComponent 中,并且具有主题属性(本质上只是一个应用于其中一个包装元素的类(,如下所示:

在 base.component 中.html

...
<div [ngClass]="theme" class="theme-container">
<!-- other components of application here -->
</div>
...

我必须用我的主题代码解决的最后一个问题是设置Bootstrap模型的样式,这些模型在运行时添加到我的应用程序中,并且由于我的主题的类仅适用于我的BaseComponent,显然没有样式应用于模态。

例如,假设我有一个深色主题和一个浅色主题,这是一个典型的场景。如何将此类样式应用于其内容添加到应用程序正文的模式?

ngb-modal-window将插入<body>标签,因此您应该使用角度renderer将主题CSS类添加到正文标签中

@Component({
selector: 'my-app',
templateUrl: './app.component.html'
})
export class AppComponent {
theme = "my-theme";
constructor(private renderer: Renderer2) {
this.renderer.addClass(document.body, this.theme);
}
}

最新更新