Arshad Mehmood

Search the blog

    Today I Learned

    Add a Window Resize event listener for a component

    angular2

    Register a window resize event listener within a component:

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
      host: {
        '(window:resize)': 'onResize($event)'
      }
    })
    export class AppComponent{
       onResize(event){
         console.log(event.target.innerWidth); // window width
       }
    }

    There are other ways also, I got the answer from here.