Last active
July 13, 2018 18:29
-
-
Save faisalmuhammad/0812d09e78093083cbc43b9c7ae32bd1 to your computer and use it in GitHub Desktop.
Angular | Development Only Directive
This file contains hidden or 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
/** | |
* @author Muhammad FAISAL | |
* @description Controls the display of html contents only during development mode. | |
* @example <button development-only>Some Button</button> | |
*/ | |
// Angular Imports | |
import { Directive, OnInit, ElementRef, isDevMode } from '@angular/core'; | |
// Define class for the Directive | |
@Directive({ | |
selector: '[development-only]' | |
}) | |
export class DevelopmentOnlyDirective implements OnInit { | |
/** | |
* Initializes an instance of DevelopmentOnlyDirective class. | |
* @param _elementRef Reference to the element where this directive is applied. | |
*/ | |
constructor(private readonly _elementRef: ElementRef) { } | |
/** | |
* Invoked when component initializes. | |
*/ | |
ngOnInit() { | |
const isDevelopment = isDevMode(); | |
if (!isDevelopment && this._elementRef.nativeElement.parentNode !== null) { | |
this._elementRef.nativeElement.outerHTML = ''; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment