Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robertdean/5d5e26912e238fdafbd827d0c1c97002 to your computer and use it in GitHub Desktop.
Save robertdean/5d5e26912e238fdafbd827d0c1c97002 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