I've written a directive that hides an element depending on screen size. When the page loads the element that is supposed to be hidden is being initialised.
This is my directive
mobile-only.directive.ts
@Directive({
selector: '[appMobileOnly]'
})
export class MobileOnlyDirective {
constructor(private el: ElementRef) {
this.displayElement(window.innerWidth < 992);
}
displayElement(isMobile) {
if (isMobile) {
this.el.nativeElement.style.display = 'block';
} else {
this.el.nativeElement.style.display = 'none';
}
}
}
It is implemented like this
<div appMobileOnly>
<app-brochure-mobile></app-brochure-mobile>
</div>
The directive behaves as expected in that the component isn't rendered to the screen, however the ngOnInit function in the component is being fired. Any idea why this might be happening?