Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set a variable value based on screen size in angular using bootstrap?

I have a global variable in an angular component like so,

visibleDays = 7

I want to be able to control this value based on screen size. For smaller devices, I want this to be 3 instead of 7. Is it possible to do this for "sm", "xs" devices?

like image 544
Karu Avatar asked Sep 13 '25 04:09

Karu


1 Answers

you can track windows size and base of innwewidth update the value of visibleDays

app.component

  visibleDays = 7;

  @HostListener("window:resize", []) updateDays() {
    // lg (for laptops and desktops - screens equal to or greater than 1200px wide)
    // md (for small laptops - screens equal to or greater than 992px wide)
    // sm (for tablets - screens equal to or greater than 768px wide)
    // xs (for phones - screens less than 768px wide)
  
    if (window.innerWidth >= 1200) {
      this.visibleDays = 7; // lg
    } else if (window.innerWidth >= 992) {
      this.visibleDays = 6;//md
    } else if (window.innerWidth  >= 768) {
      this.visibleDays = 5;//sm
    } else if (window.innerWidth < 768) {
      this.visibleDays = 3;//xs
    }
    
  }

stackblitz demo 🚀🚀

like image 108
Muhammed Albarmavi Avatar answered Sep 15 '25 00:09

Muhammed Albarmavi