Skip to content

Instantly share code, notes, and snippets.

@DoubleCouponDay
Last active October 8, 2019 21:46
Show Gist options
  • Select an option

  • Save DoubleCouponDay/43554b713496dce5624a5915ce3254b8 to your computer and use it in GitHub Desktop.

Select an option

Save DoubleCouponDay/43554b713496dce5624a5915ce3254b8 to your computer and use it in GitHub Desktop.
angular toast notifications
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