Рабочий стек: - https://stackblitz.com/edit/angular-jrkmhn
Я поместил свой css в папку с ресурсами, т.е. myStyles. css
CSS: -
.myDiv {
background-color: black;
}
индекс. html
<link id="myLink">
<my-app>loading</my-app>
app.component. html
<div class="myDiv">Css Will Load</div>
<button style="margin-top: 2em" (click)="changeTheme('')">Change Theme</button>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
changeTheme(themeName) {
var elem = document.getElementById('myLink');
elem.setAttribute('href','./assets/myStyles.css');
elem.setAttribute('rel','stylesheet');
elem.setAttribute('type','text/css');
}
}