Some features in my component turn on or off depend on browser size, therefore I want to check browser width on resize event. However, I could do it using OnInit method. But I n
You could just put handler on resize
event over window
object, but this will allow you to put only single resize
event, latest registered event on onresize
will work.
constructor(private ngZone:NgZone) {
window.onresize = (e) =>
{
//ngZone.run will help to run change detection
this.ngZone.run(() => {
console.log("Width: " + window.innerWidth);
console.log("Height: " + window.innerHeight);
});
};
}
To make it more angular way use @HostListener('window:resize')
inside your component, which will allow to call your resize function(on which HostListner
decorator has been mount) on resize
of window.
@HostListener('window:resize', ['$event'])
onResize(event){
console.log("Width: " + event.target.innerWidth);
}