Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 0e7d2e0

Browse files
fix(demo, media-query-status): should use async pipe with ObservableMedia
References #415.
1 parent e461d17 commit 0e7d2e0

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed
+11-22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import {Component, OnDestroy, OnInit} from '@angular/core';
2-
import {Subscription} from 'rxjs/Subscription';
1+
import {Component} from '@angular/core';
32

43
import {MediaChange} from '@angular/flex-layout';
54
import {ObservableMedia} from '@angular/flex-layout';
5+
import {Observable} from 'rxjs/Observable';
66

77
@Component({
88
moduleId: module.id,
99
selector : 'media-query-status',
1010
template : `
11-
<div class="mqInfo" >
12-
<span title="Active MediaQuery">{{ activeMediaQuery }}</span>
11+
<div class="mqInfo" *ngIf="change$ | async as change">
12+
<span title="Active MediaQuery">{{ buildMQInfo(change) }}</span>
1313
</div>
1414
`,
1515
styles: [
@@ -28,27 +28,16 @@ import {ObservableMedia} from '@angular/flex-layout';
2828
}`
2929
]
3030
})
31-
export class MediaQueryStatus implements OnDestroy, OnInit {
32-
private _watcher: Subscription;
33-
activeMediaQuery: string;
31+
export class MediaQueryStatus {
32+
public change$: Observable<MediaChange> = this.media$.asObservable();
3433

3534
constructor(private media$: ObservableMedia) {
36-
37-
}
38-
39-
ngOnInit() {
40-
this.watchMediaQueries();
41-
}
42-
43-
ngOnDestroy() {
44-
this._watcher.unsubscribe();
4535
}
4636

47-
private watchMediaQueries() {
48-
this._watcher = this.media$.subscribe((change: MediaChange) => {
49-
if (change.mediaQuery.indexOf('orientation') > -1) { return; }
50-
let value = change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
51-
this.activeMediaQuery = value;
52-
});
37+
public buildMQInfo(change: MediaChange): string {
38+
if (change.mediaQuery.indexOf('orientation') > -1) {
39+
return '';
40+
}
41+
return change ? `'${change.mqAlias}' = (${change.mediaQuery})` : '';
5342
}
5443
}

0 commit comments

Comments
 (0)