Detecting real time window size changes in Angular 4

后端 未结 9 722
孤独总比滥情好
孤独总比滥情好 2020-12-04 05:44

I have been trying to build a responsive nav-bar and do not wish to use a media query, so I intend to use *ngIF with the window size as a criterion. But I have

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 06:51

    To get it on init

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

    If you wanna keep it updated on resize:

    @HostListener('window:resize', ['$event'])
    onResize(event) {
      this.innerWidth = window.innerWidth;
    }
    

提交回复
热议问题