角度材质2.单击时将主题从亮到暗切换

时间:2017-02-27 09:11:33

标签: angular typescript material-design angular-material2

所以我有一个Angular 2 Material应用程序。 而我想要做的只是通过点击简单button来切换/切换主题从黑暗到光明。

我该怎么做?

1 个答案:

答案 0 :(得分:8)

menu

<强> app.component.html

<div [class.dark-theme]="isDarkTheme">
    <!--Your application content here-->
    <md-menu #more="mdMenu">
        <!--Your content here-->
        <button md-menu-item (click)="changeTheme()">
            Change Theme
        </button>
    </md-menu>
</div>

<强> app.component.ts

// import statements here
import {Component} from '@angular/core';

export class AppComponent {
    // Initialize isDarkTheme to false
    isDarkTheme: boolean = false;
    // Your code here

    changeTheme(): void {
        if (this.isDarkTheme) {
           this.isDarkTheme = false;
        } else {
           this.isDarkTheme = true;
        }
     }
}

<强> theme.scss

@import '~@angular/material/core/theming/_all-theme';

@include mat-core();
.dark-theme {
    // Dark theme
    $app-dark-primary: mat-palette($mat-pink, 700);
    $app-dark-accent: mat-palette($mat-blue-grey);
    $app-dark-theme: mat-dark-theme($app-dark-primary, $app-dark-accent);

    @include angular-material-theme($app-dark-theme);

}