I have a number of elements that I want to be visible under certain conditions.
In AngularJS I would write
stuff
For anybody else stumbling across this issue, this is how I accomplished it.
import {Directive, ElementRef, Input, OnChanges, Renderer2} from "@angular/core";
@Directive({
selector: '[hide]'
})
export class HideDirective implements OnChanges {
@Input() hide: boolean;
constructor(private renderer: Renderer2, private elRef: ElementRef) {}
ngOnChanges() {
if (this.hide) {
this.renderer.setStyle(this.elRef.nativeElement, 'visibility', 'hidden');
} else {
this.renderer.setStyle(this.elRef.nativeElement, 'visibility', 'visible');
}
}
}
I used 'visibility'
because I wanted to preserve the space occupied by the element. If you did not wish to do so, you could just use 'display'
and set it to 'none'
;
You can bind it to your html element, dynamically or not.
or