Detecting real time window size changes in Angular 4

后端 未结 9 697
孤独总比滥情好
孤独总比滥情好 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:31

    you can use this https://github.com/ManuCutillas/ng2-responsive Hope it helps :-)

    0 讨论(0)
  • 2020-12-04 06:31

    The answer is very simple. write the below code

    import { Component, OnInit, OnDestroy, Input } from "@angular/core";
    // Import this, and write at the top of your .ts file
    import { HostListener } from "@angular/core";
    
    @Component({
     selector: "app-login",
     templateUrl: './login.component.html',
     styleUrls: ['./login.component.css']
    })
    
    export class LoginComponent implements OnInit, OnDestroy {
    // Declare height and width variables
    scrHeight:any;
    scrWidth:any;
    
    @HostListener('window:resize', ['$event'])
    getScreenSize(event?) {
          this.scrHeight = window.innerHeight;
          this.scrWidth = window.innerWidth;
          console.log(this.scrHeight, this.scrWidth);
    }
    
    // Constructor
    constructor() {
        this.getScreenSize();
    }
    }
    
    0 讨论(0)
  • 2020-12-04 06:34
    @HostListener("window:resize", [])
    public onResize() {
      this.detectScreenSize();
    }
    
    public ngAfterViewInit() {
        this.detectScreenSize();
    }
    
    private detectScreenSize() {
        const height = window.innerHeight;
        const width = window.innerWidth;
    }
    
    0 讨论(0)
  • 2020-12-04 06:37

    You may use the typescript getter method for this scenario. Like this

    public get width() {
      return window.innerWidth;
    }
    

    And use that in template like this:

    <section [ngClass]="{ 'desktop-view': width >= 768, 'mobile-view': width < 768 
    }"></section>
    

    You won't need any event handler to check for resizing/ of window, this method will check for size every time automatically.

    0 讨论(0)
  • 2020-12-04 06:41

    If you want to react on certain breakpoints (e.g. do something if width is less than 768px), you can also use BreakpointObserver:

    import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
    
    { ... }
    
    const isSmallScreen = breakpointObserver.isMatched('(max-width: 599px)');
    

    or even listen to changes to that breakpoint:

    breakpointObserver.observe([
      '(max-width: 768px)'
        ]).subscribe(result => {
          if (result.matches) {
            doSomething();
          } else {
            // if necessary:
            doSomethingElse();
          }
        });
    
    0 讨论(0)
  • 2020-12-04 06:47

    If you'd like you components to remain easily testable you should wrap the global window object in an Angular Service:

    import { Injectable } from '@angular/core';
    
    @Injectable()
    export class WindowService {
    
      get windowRef() {
        return window;
      }
    
    }
    

    You can then inject it like any other service:

    constructor(
        private windowService: WindowService
    ) { }
    

    And consume...

      ngOnInit() {
          const width= this.windowService.windowRef.innerWidth;
      }
    
    0 讨论(0)
提交回复
热议问题