检查material2 sidenav是否打开

时间:2017-01-11 12:35:12

标签: angular material-design angular-material angular-material2

有没有办法检查sidenav元素是否已打开?根据{{​​3}}文档,有一个isOpen?参数,但在我的情况下会引发异常: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();
     }
  }

}

2 个答案:

答案 0 :(得分:6)

您必须使用ViewChild来控制sidenav

@ViewChild('sidenav') sidenav: any;
opened: any = false;
onClick() {
   console.log(this.sidenav.opened);
}

Plunkr

答案 1 :(得分:2)

你误解了文档,isOpen?是切换功能的可选参数。

您可以检查它的属性。这对我有用:

BrowserHandler