Angular 2/4 How to style angular material design snackbar

前端 未结 7 1869
广开言路
广开言路 2021-02-03 22:22

I am new to Angular2/4 and angular typescript. I want to style the angular material design snackbar for example change the background color from black and font

7条回答
  •  后悔当初
    2021-02-03 23:01

    (Angular 9 at the moment) Hi, here is a summary of the two links at the bottom. You get here the call function with css class and variables passed and the way to get their values in the snackBar component. Hope this helps

    In the component where you wish to rise a snackBar, the "father component" :

    import {
      MatSnackBar
    } from '@angular/material/snack-bar';
    import {
      SnackBarComponent
    } from '../../../shared/snack-bar/snack-bar.component';
    
    
    @Component({
      selector: 'app-father-component',
      templateUrl: './father-.component.html',
      styleUrls: ['./father-editor.component.scss'],
    })
    export class FatherComponent implements OnInit {
    
      private _durationInSeconds = 5;
    
      get durationInSeconds(): number {
        return this._durationInSeconds;
      }
    
      set durationInSeconds(value: number) {
        this._durationInSeconds = value;
      }
    
      private _className: string;
    
      get className(): string {
        return this._className;
      }
    
      set className(value: string) {
        this._className = value;
      }
    
      private _mMessage: string;
    
      get mMessage(): string {
        return this._mMessage;
      }
    
      set mMessage(value: string) {
        this._mMessage = value;
      }
    
      constructor(
        private snackBar: MatSnackBar
      ) {}
    
      ngOnInit(): void {
    
        // your onInit code
      }
    
    // Calling that function rises the snackBar, from .ts this.openSnackBar()
    // From template event  etc...
    // No needs to insert the  tag in the template.
    
      openSnackBar() {
        this.snackBar.openFromComponent(SnackBarComponent, {
          duration: this.durationInSeconds * 1000,
          data: {
            myMessage: this.mMessage,
            antoherVar: 'antoher message'
          },
          // here this.className is a string which value is a Bulma class 'has-text-info', snackbar automatically uses the value from panelClass
          panelClass: this.className
        });
      }

    Then the component which is the snackBar itSelf, the "child component" :

    import {
      Component,
      Inject,
      OnInit
    } from '@angular/core';
    import {
      MAT_SNACK_BAR_DATA
    } from '@angular/material/snack-bar';
    
    @Component({
      selector: 'app-snack-bar',
      templateUrl: './snack-bar.component.html',
      styleUrls: ['./snack-bar.component.scss'],
    })
    export class SnackBarComponent implements OnInit {
    
      myMessage: string;
      antoherVar: string;
    
      constructor(
        @Inject(MAT_SNACK_BAR_DATA) public data: any
      ) {
      // there you get the datas you have passed to the snackbar through the openSnackBar function in the father-component, setting up local properties to their values
        this.myMessage = data.myMessage;
        this.antoherVar = data.antoherVar;
      }
    
      ngOnInit(): void {}
    
    }
    
    
    {{ myMessage }}

提交回复
热议问题