2

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

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();
    }
}
4

1 回答 1

2

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

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

在你的组件中

test(start: any) {
        start.toggle();
    }
于 2016-10-26T14:14:16.357 回答