Angular2 通过组件代码控制材质

Angular2 material control through component code

我想在调用test()方法时启动start.toggle()函数(属于Angular 2 material md-sidenav-layout组件)。 如何在app.component.ts?

中调用md-sidenav-layout start.toggle()

app.component.html

<md-sidenav-layout class="sidenav" fullscreen>
    <md-sidenav #start>
        Start Side Drawer
        <br>
        <md-nav-list>
            <a md-list-item [routerLink]="['']">Home</a>
            <a md-list-item [routerLink]="['about']">About</a>
        </md-nav-list>
        <button md-button (click)="start.close()">Close</button>
    </md-sidenav>

    <div class="content">
        <button md-button (click)="start.toggle()">Toggle Start Side Drawer</button>

        <button md-button (click)="test()">Test Button</button>
        <main>
            <router-outlet></router-outlet>
        </main>
        <footer>
        </footer>
    </div>
</md-sidenav-layout>

app.component.ts

import {Component} from '@angular/core';
// what to import here?

@Component({
    selector: 'my-app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
})
export class AppComponent {
    constructor() {
    }

    test() {
        // how to call md-sidenav-layout start.toggle() properly here?
        start.toggle();
    }
}

将该对象传递给您的函数,以便您可以使用它。

<button md-button (click)="test(start)">Test Button</button>

在你的组件中

test(start: any) {
        start.toggle();
    }