Last active
October 8, 2019 21:46
-
-
Save DoubleCouponDay/43554b713496dce5624a5915ce3254b8 to your computer and use it in GitHub Desktop.
angular toast notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { MatSnackBar } from '@angular/material'; | |
| import { HttpErrorResponse } from '@angular/common/http'; | |
| import { Injectable } from '@angular/core'; | |
| import { isnullorundefined } from '../utility/utilities'; | |
| /** requires @angular/material, MatSnackBarModule, isnullorundefined */ | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export default class snackbarservice | |
| { | |
| constructor(private snackBar: MatSnackBar) | |
| { | |
| } | |
| public raisemessage = (message: string, title?:string) : void => { | |
| this.underlyingraise(message, title) | |
| } | |
| public raiseerror = (error: Error) : void => { | |
| let message: string | |
| if(error instanceof HttpErrorResponse) | |
| { | |
| let castasresponse = <HttpErrorResponse>error | |
| message = `${castasresponse.status}: ${castasresponse.message}` | |
| } | |
| else | |
| { | |
| message = isnullorundefined(error.message) ? <any>error : error.message | |
| } | |
| this.underlyingraise(message, null) | |
| } | |
| private underlyingraise = (message: string, title?:string) : void => { | |
| let titletouse = isnullorundefined(title) === false ? title : '' | |
| let output = `${titletouse}: ${JSON.stringify(message)}` | |
| this.snackBar.open(output, '', { | |
| duration: 999999999, | |
| verticalPosition: "top", | |
| horizontalPosition: "right" | |
| }); | |
| console.log(output) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment