Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using template in angular 2 based on window size

On my template, I have 2 divs. one for table view(laptop) and one for list view(mobile view).

In my .ts file, I want to get the window size so that based on this size, I can choose which div i want to call using ngIf.

In the constructor of the .ts file, I have:

let windowSize: number; 
this.windowSize = innerWidth + innerHeight;
window.addEventListener("resize", function() {
    this.windowSize = innerWidth + innerHeight";
});

But when i console.log() this.windowSize, I dont get the change when the window is resized. What am i missing?

like image 451
user3344978 Avatar asked Sep 18 '26 04:09

user3344978


1 Answers

This can be set with simple css:

 @media (min-width: 500px) and (max-width: 700px) {
    .div1 {
      display: none;
    }

 @media (min-width: 701px) and (max-width: 900px) {
    .div2 {
      display: none;
    }
like image 112
Avi Avatar answered Sep 19 '26 19:09

Avi