Skip to content

Commit

Permalink
fix(demo, media-query-status): should use async pipe with ObservableM…
Browse files Browse the repository at this point in the history
…edia

References #415.
  • Loading branch information
ThomasBurleson committed Sep 19, 2017
1 parent e461d17 commit 0e7d2e0
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions src/demo-app/app/shared/media-query-status.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {Component, OnDestroy, OnInit} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';
import {Component} from '@angular/core';

import {MediaChange} from '@angular/flex-layout';
import {ObservableMedia} from '@angular/flex-layout';
import {Observable} from 'rxjs/Observable';

@Component({
moduleId: module.id,
selector : 'media-query-status',
template : `
<div class="mqInfo" >
<span title="Active MediaQuery">{{ activeMediaQuery }}</span>
<div class="mqInfo" *ngIf="change$ | async as change">
<span title="Active MediaQuery">{{ buildMQInfo(change) }}</span>
</div>
`,
styles: [
Expand All @@ -28,27 +28,16 @@ import {ObservableMedia} from '@angular/flex-layout';
}`
]
})
export class MediaQueryStatus implements OnDestroy, OnInit {
private _watcher: Subscription;
activeMediaQuery: string;
export class MediaQueryStatus {
public change$: Observable<MediaChange> = this.media$.asObservable();

constructor(private media$: ObservableMedia) {

}

ngOnInit() {
this.watchMediaQueries();
}

ngOnDestroy() {
this._watcher.unsubscribe();
}

private watchMediaQueries() {
this._watcher = this.media$.subscribe((change: MediaChange) => {
if (change.mediaQuery.indexOf('orientation') > -1) { return; }
let value = change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
this.activeMediaQuery = value;
});
public buildMQInfo(change: MediaChange): string {
if (change.mediaQuery.indexOf('orientation') > -1) {
return '';
}
return change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
}
}

0 comments on commit 0e7d2e0

Please sign in to comment.