I'm trying to render a div based on whether or not the screen is a certain size. I am able to do this when the page initially loads, however I'm not understanding how to do so when the window changes size AFTER the page loads. I would like a div to either be a "mobile" div or "!mobile" div depending on current screen size.
Here is the relevant code for my home.component.ts file:
import { Component, OnInit, HostListener } from '@angular/core';
export class HomeComponent implements OnInit {
mobile: Boolean;
}
ngOnInit() {
if (window.screen.width <= 615) {
this.mobile = true;
} else if (window.screen.width >= 616) {
this.mobile = false;
}
@HostListener('window:resize', ['$event'])
onResize(event) {
if (event.target.innerWidth <= 767) {
this.mobile = true;
this.ngOnInit();
} else if (event.target.innerWidth >= 768) {
this.mobile = false;
this.ngOnInit();
}
}
and here is my home.component.html file div's:
<ngb-carousel (window:resize)="onResize($event) *ngIf="mobile">
<!-- ...additional content within mobile div -->
</ngb-carousel>
<div (window:resize)="onResize($event) *ngIf="!mobile" class="homepage-img-card row ml-5">
<!-- ...additional content within non-mobile div -->
</div>
Not sure how to correctly bind what's happening in the HostListener to the html in order to render the desired div based on the current window size. I know the HostListener part is working, since I'm able to console.log the current screen size (not included in my example).