app.component.ts 887 Bytes
Newer Older
1
2
import { Component, ChangeDetectionStrategy, ChangeDetectorRef, OnDestroy } from '@angular/core';
import { MediaMatcher } from '@angular/cdk/layout';
Andrew Hrdy's avatar
Andrew Hrdy committed
3
4

@Component({
5
6
7
8
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
Andrew Hrdy's avatar
Andrew Hrdy committed
9
})
10
11
12
export class AppComponent implements OnDestroy {
    private mobileQuery: MediaQueryList;
    private mobileQueryListener;
Andrew Hrdy's avatar
Andrew Hrdy committed
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    constructor(cd: ChangeDetectorRef, media: MediaMatcher) {
        this.mobileQuery = media.matchMedia('(max-width: 600px)');

        this.mobileQueryListener = () => cd.detectChanges();

        this.mobileQuery.addListener(this.mobileQueryListener);
    }

    get isMobile(): boolean {
        return this.mobileQuery.matches;
    }

    ngOnDestroy() {
        this.mobileQuery.removeListener(this.mobileQueryListener);
    }
Andrew Hrdy's avatar
Andrew Hrdy committed
29
}