2

I have div element which I want to show or no depending on the screen size (I know that can be done by mediaqueries, but tried to approach this with Angular). So I got working NgStyle condition [ngStyle]="{display: innerWidth < '600' ? 'none' : 'flex'}" when innerWidth is variable in the .ts file of this component

public innerWidth: any;
  ngOnInit() {
    this.innerWidth = window.innerWidth;
  }

This is working fine, but changing only when I reload the page, so I want to trigger this change dynamically when I resize the browser window, is that possible in similar way?

1
  • 1
    You basically need to subscribe to window's resize event and set value for this.innerWidth when that event occurs. Checkout here stackoverflow.com/a/45451974/3965832 Commented Dec 26, 2019 at 19:05

1 Answer 1

5

You'll need to update the value when the window is resized. You can listen for window:resize:

@HostListener('window:resize', ['$event'])
onResize(event) {
  this.innerWidth = event.target.innerWidth;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.