有没有办法检查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();
}
}
}
答案 0 :(得分:6)
您必须使用ViewChild
来控制sidenav
@ViewChild('sidenav') sidenav: any;
opened: any = false;
onClick() {
console.log(this.sidenav.opened);
}
答案 1 :(得分:2)
你误解了文档,isOpen?是切换功能的可选参数。
您可以检查它的属性。这对我有用:
BrowserHandler