Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get initial resize of window for Angular2 app component

Tags:

angular

In my app.html I have:

<div id="wrapper" 
    (document:load)="onWindowLoadOrResize($event)" 
    (window:resize)="onWindowLoadOrResize($event)">

This is used to set the height on the css for a content div. The resize is working fine, but it does not fire initially on the page load. The document:load does not fire at all. What is the best way to run code that will see the initial size of the window?

like image 629
Peter Avatar asked Mar 01 '26 17:03

Peter


2 Answers

At this point in your application, document.load is already fired. Easiest for you would probably be to do call the resizing function from your app-components constructor. At this point your DOM is ready, and the application is firing up. Also, you should assign the height value to a property in your app that the template can bind to rather than do DOM manipulation directly.

like image 165
jornare Avatar answered Mar 03 '26 17:03

jornare


export class Application implements OnInit {
    ngOnInit() {
        window.onresize = this.onWindowLoadOrResize;
        this.onWindowLoadOrResize();
    }
    private onWindowLoadOrResize() {
      //TODO: your logic        
    }
}
like image 21
Natarajan Ganapathi Avatar answered Mar 03 '26 17:03

Natarajan Ganapathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!