xxxxxxxxxx
<button (click)="applyCssRules('body { background-color: red; }')">Apply CSS Rules</button>
xxxxxxxxxx
applyCssRules(cssRules: string) {
// Sanitize and apply the CSS rules
const sanitizedCssRules = this.sanitizer.bypassSecurityTrustStyle(cssRules);
// Insert the sanitized CSS rules into the <style> tag
const styleElement = document.querySelector('style');
if (styleElement) {
styleElement.innerHTML = sanitizedCssRules.toString();
}
}
xxxxxxxxxx
import { DomSanitizer } from '@angular/platform-browser';
constructor(private sanitizer: DomSanitizer) {}