我的應用程序中有一個材料2 sidenav組件。我如何從我的課程訪問組件方法?在我的情況下,我想調用open()
方法。在模板與<button md-button (click)="sidenav.open()">
完全正常工作,但如果我通過this.el.nativeElement.open();
使用的方法在課堂上,它拋出:來自類的調用組件的方法
ERROR TypeError: Cannot read property 'open' of undefined
我的組件:
import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
@Component({
selector: 'app-sidebar-content',
template: `
<md-sidenav-container class="example-container">
<md-sidenav #sidenav class="example-sidenav">
Jolly good!
</md-sidenav>
<div class="example-sidenav-content">
<button md-button (click)="sidenav.open()">
Open sidenav
</button>
</div>
</md-sidenav-container>
`,
styleUrls: ['./sidebar-content.component.css']
})
export class SidebarContentComponent implements OnInit {
@ViewChild('sidenav') el: ElementRef;
constructor() { }
ngOnInit() {
}
openSideNav() {
// this.el.nativeElement.open();
// ERROR TypeError: Cannot read property 'open' of undefined
}
}