You can use @ angular / flex-alyout ObservableMedia to view media / interrupt changes and updates mode
or other properties, respectively, depending on the active media level. This is done by subscribing to an inclinated instance of the service ObservableMedia
and checking the active media level. Then you can bind opened
and mode
properties to <md-sidenav>
by linking []
to your class properties. This logic can be placed in services and / or attribute directives, if necessary.
Using / updating properties instead of applying CSS changes ensures that appropriate animations occur.
TS:
import { Component } from '@angular/core';
import { MediaChange, ObservableMedia } from "@angular/flex-layout";
@Component({ ... })
export class AppComponent {
mode: string = 'side';
opened: boolean = true;
constructor(private media: ObservableMedia) {
this.media.subscribe((mediaChange: MediaChange) => {
this.mode = this.getMode(mediaChange);
});
}
private getMode(mediaChange: MediaChange): string {
if (this.media.isActive('gt-sm')) {
return 'side';
} else {
return 'over';
}
}
private getOpened(mediaChange: MediaChange): string { }
}
HTML:
<md-sidenav-container class="example-container">
<md-sidenav #sidenav class="example-sidenav" [opened]="opened" [mode]="mode">
Jolly good!
</md-sidenav>
<div class="example-sidenav-content">
<p>Sidenav content</p>
<button type="button" md-button (click)="sidenav.open()">
Open sidenav
</button>
</div>
</md-sidenav-container>
Hope this helps!
source
share