Skip to content

Instantly share code, notes, and snippets.

@junaid1460
Created January 6, 2019 09:39
Show Gist options
  • Save junaid1460/840b4d85c30a123a5e4d695b925888c6 to your computer and use it in GitHub Desktop.
Save junaid1460/840b4d85c30a123a5e4d695b925888c6 to your computer and use it in GitHub Desktop.
import { Directive, Input, OnInit, ElementRef, Renderer2 } from '@angular/core';
import { ThemeService } from './theme.service';
@Directive({
selector: '[appTheme]'
})
export class ThemeDirective implements OnInit {
@Input('appTheme') appTheme: string; // appTheme="customClassKey"
constructor(
private themeService: ThemeService,
private el: ElementRef,
private renderer: Renderer2) {
}
ngOnInit() {
const name = this.appTheme || this.el.nativeElement.localName;
const className = this.themeService.getClass(name); // get class name from theme service
if (className) {
this.renderer.addClass(this.el.nativeElement, className); // apply class
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment