Skip to content

Instantly share code, notes, and snippets.

@faisalmuhammad
Last active July 13, 2018 18:29
Show Gist options
  • Save faisalmuhammad/0812d09e78093083cbc43b9c7ae32bd1 to your computer and use it in GitHub Desktop.
Save faisalmuhammad/0812d09e78093083cbc43b9c7ae32bd1 to your computer and use it in GitHub Desktop.
Angular | Development Only Directive
/**
* @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