1
- import { Component , OnDestroy , OnInit } from '@angular/core' ;
2
- import { Subscription } from 'rxjs/Subscription' ;
1
+ import { Component } from '@angular/core' ;
3
2
4
3
import { MediaChange } from '@angular/flex-layout' ;
5
4
import { ObservableMedia } from '@angular/flex-layout' ;
5
+ import { Observable } from 'rxjs/Observable' ;
6
6
7
7
@Component ( {
8
8
moduleId : module . id ,
9
9
selector : 'media-query-status' ,
10
10
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>
13
13
</div>
14
14
` ,
15
15
styles : [
@@ -28,27 +28,16 @@ import {ObservableMedia} from '@angular/flex-layout';
28
28
}`
29
29
]
30
30
} )
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 ( ) ;
34
33
35
34
constructor ( private media$ : ObservableMedia ) {
36
-
37
- }
38
-
39
- ngOnInit ( ) {
40
- this . watchMediaQueries ( ) ;
41
- }
42
-
43
- ngOnDestroy ( ) {
44
- this . _watcher . unsubscribe ( ) ;
45
35
}
46
36
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 } )` : '' ;
53
42
}
54
43
}
0 commit comments