*ngIf hide some content on mobile screen, Angular 4

后端 未结 9 2177
盖世英雄少女心
盖世英雄少女心 2020-12-13 04:47

I have:

showHide: false;

Content
9条回答
  •  悲&欢浪女
    2020-12-13 05:20

    For future me or others if you need a solution that monitors the screen size and are using Observables/rxjs then this solution works:

    ngOnInit(): void {
      // Checks if screen size is less than 1024 pixels
      const checkScreenSize = () => document.body.offsetWidth < 1024;
    
      // Create observable from window resize event throttled so only fires every 500ms
      const screenSizeChanged$ = Observable.fromEvent(window, 'resize').throttleTime(500).map(checkScreenSize);
    
      // Start off with the initial value use the isScreenSmall$ | async in the
      // view to get both the original value and the new value after resize.
      this.isScreenSmall$ = screenSizeChanged$.pipe(startWith(checkScreenSize()))
    }
    

提交回复
热议问题