Skip to content

Instantly share code, notes, and snippets.

@DuncanFaulkner
Last active July 28, 2021 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DuncanFaulkner/94a71cf6e41931f8c86891025a8eb4f1 to your computer and use it in GitHub Desktop.
Save DuncanFaulkner/94a71cf6e41931f8c86891025a8eb4f1 to your computer and use it in GitHub Desktop.
mediaObserver
import { Component, OnInit } from '@angular/core';
import { MediaChange, MediaObserver } from '@angular/flex-layout';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
constructor(private mediaObserver: MediaObserver) {}
cols: Observable<any> | undefined;
ngOnInit(): void {
const grid = new Map([
['xs', 1],
['sm', 2],
['md', 3],
['lg', 4],
['xl', 5],
]);
this.cols = this.mediaObserver.asObservable().pipe(
map((change: MediaChange[]) => {
console.log(change[0]);
console.log(grid.get(change[0].mqAlias));
return grid.get(change[0].mqAlias);
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment