Created
January 6, 2019 09:39
-
-
Save junaid1460/840b4d85c30a123a5e4d695b925888c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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