Is it possible to have multiple actions within an Angular Material snackbar?
I know it is possible to use your own component for the snackbar.
But for this code, the action is only one action
private snackBar: MatSnackBar;
this.snackBar._openedSnackBarRef.onAction().subscribe(
() => {
console.log('action has occurred, but which one?')
}
);
Angular Material 7 - SnackBar. The <MatSnackBar>, an Angular Directive, is used to show a notification bar to show on mobile devices as an alternative of dialogs/popups. In this chapter, we will showcase the configuration required to show a snack bar using Angular Material.
Angular Material Snackbar or Angular material toast is useful for showing alert and notification messages. Exploring how to implement Angular material toast | snackbar and how to configure its color and position. How to apply style or customize the style to Angular material toast | snackbar.
Position the snackBar on page screen and it can have values of “start” or “left”, “center”, “end” or “right”. How to apply Angular Material Snackbar color? We can apply CSS classes to our Angular material toast, by using the CSS custom class name to panelClass attribute and we can add style and in our let change the background color.
Step 1: Let’s first create an Angular material snack project, we need to install the angular material library. Let’s create and install the angular material library. ng new snackMaterialApp cd snackMaterialApp ng add @angular/material While running the 3rd command will ask you the select theme, material typography, and animation, press yes on all.
I am using Angular9 and we have an option to use a custom component inside the SnackBar
. Here is the sample code:
Inside callee component:
openSnackBar(message) {
this.snackBar.openFromComponent(CustomSnackBarComponent, {
duration: 5 * 1000,
});
}
Custom component:
@Component({
selector: 'snack-bar-component-example-snack',
template: `<div class="example-pizza-party">
<button mat-raised-button (click)="cancel()">Cancel </button>
<button mat-raised-button (click)="doAction()">View Results </button>
</div>`,
styles: [`
.example-pizza-party {
color: green;
}
`],
})
export class CustomSnackBarComponent {
constructor(private router: Router) { }
doAction() {
// you can do some action here
}
cancel() {
// close the snackbar or wirte another action
}
}
Refer to the official docs here.
Demo : SnackBar created using Bottom Sheet
I have not found 2 actions working in SnackBars, but implemented a snackBar with 2 actions using BottomSheet Component.
Scenario :
I had same requirement in the project.
Notifications were displayed using snackBar in the whole project, and for one action, its easy to handle using snackBar.
Problem :
Solution Achieved :
Opened SnackBar :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With