Is there a way to check if the sidenav
element is open? According the API documentation, there is a isOpen?
parameter, but in my case it throws an exception: EXCEPTION: Error in ./AppComponent class AppComponent - inline template:2:2 caused by: this.sidenav.isOpen is not a function
import { Component, OnInit, ElementRef } from '@angular/core';
@Component({
selector: 'app-sidebar',
template: `
<md-sidenav #sidenav mode="side" class="app-sidenav">
Sidenav
</md-sidenav>
`,
styleUrls: ['./sidebar.component.css'],
host: {
'(document:click)': 'onClick($event)',
}
})
export class SidebarComponent implements OnInit {
constructor(private sidenav: ElementRef) { }
ngOnInit() {
}
onClick() {
if (this.sidenav.isOpen()) {
this.sidenav.close();
}
}
}
From the docs: A mat-sidenav can be opened or closed using the open(), close() and toggle() methods. Each of these methods returns a Promise that will be resolved with true when the sidenav finishes opening or false when it finishes closing.
Opening and closing a sidenav A <mat-sidenav> can be opened or closed using the open() , close() and toggle() methods.
The <mat-sidenav>, an Angular Directive, is used to create a side navigation bar and main content panel with material design styling and animation capabilities. <mat-sidenav-container> - Represents the main container. <mat-sidenav-content> - Represents the content panel. <mat-sidenav> - Represents the side panel.
MatDrawer. This component corresponds to a drawer that can be opened on the drawer container.
You have to use ViewChild
to get control over sidenav
@ViewChild('sidenav') sidenav: any;
opened: any = false;
onClick() {
console.log(this.sidenav.opened);
}
Plunkr
You misunderstand the documentation, isOpen? is an optional parameter of the toggle function.
You can check it's property instead. This works for me:
@ViewChild('sidenav') sidenav: MatSidenav;
onClick() {
console.log(`isOpen: ${this.sidenav.opened}`);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With