Angular Material Tutorial - 25 - Snackbar
Summary
TLDRThis video tutorial introduces snack bars in Angular Material, which are notification tools used to inform users of completed actions. It covers how to import the necessary modules, create a button to trigger a snack bar, and define methods for opening it with customizable messages and actions. The video also explains handling user interactions with observables, setting durations for automatic dismissal, and creating custom snack bar content with components. The comprehensive guide is aimed at developers looking to enhance their Angular applications with user-friendly notifications.
Takeaways
- π Snack bars in Angular Material are used as notifications to inform users of performed operations.
- π΄ They appear at the bottom of the screen and disappear automatically without requiring user input.
- π« Only one snack bar notification can be displayed at a time.
- π οΈ To implement a snack bar, import the MatSnackBarModule and add it to the NgModule's imports array.
- π Add a button with the mat-button attribute and handle the click event to trigger the opening of a snack bar.
- π In the component class, inject MatSnackBar through the constructor and define an openSnackBar method.
- π§ Pass a message and an optional action to the open method of the MatSnackBar service to display the snack bar with an action button.
- π Use observables like afterDismissed() and onAction() to execute code when the snack bar is dismissed or the action is clicked.
- β±οΈ The duration of a snack bar's display can be set using the duration property in milliseconds.
- π¨ Custom components can be used as content for a snack bar with the openFromComponent() method.
- π Snack bars can be utilized for actions such as undoing an operation, enhancing user experience.
Q & A
What are snack bars in Angular Material?
-Snack bars in Angular Material are notifications that inform users of some operation that has been performed. They appear for a short duration at the bottom of the screen and do not require user input to disappear.
How to import the MatSnackBarModule in Angular?
-To import the MatSnackBarModule, you need to include it from 'material.module.es' and add it to the 'imports' array in your module file.
How do you add a button to trigger a snack bar in Angular?
-You can add a button with the 'mat-button' attribute and handle the 'click' event to call a method that opens the snack bar, passing a message as an argument.
What service is used to open a snack bar in Angular?
-The 'MatSnackBar' service from 'angular/material' is used to open a snack bar in Angular applications.
How can you add an action to a snack bar?
-An action can be added to a snack bar as the second argument to the 'open' method on the service instance. The action is displayed next to the message and can be clicked to execute a function.
What are observables in the context of snack bars in Angular?
-Observables related to snack bars are used to execute code when the snack bar is dismissed or when an action is clicked. 'After dismissed' and 'on action' are two such observables.
How do you handle the action click on a snack bar?
-To handle the action click, you subscribe to the 'on action' observable of the snack bar instance, which allows you to execute code when the action is clicked.
What is the purpose of the 'duration' property in snack bar configuration?
-The 'duration' property in snack bar configuration indicates the length of time in milliseconds before the snack bar is automatically dismissed.
How can you create a custom snack bar with a component in Angular?
-To create a custom snack bar, you can use the 'open from component' method of the MatSnackBar service, passing in the custom component as its first argument along with a duration for how long the snack bar should be displayed.
What is the typical use case for an action in a snack bar?
-A typical use case for an action in a snack bar is to provide an 'undo' option for an operation, allowing users to revert the action that was performed.
How do you structure the component class to handle snack bars?
-In the component class, you would inject the MatSnackBar service into the constructor and define methods to open snack bars, handle actions, and possibly execute code after the snack bar is dismissed or an action is clicked using observables.
Outlines
π’ Introduction to Angular Material Snackbars
This paragraph introduces the concept of snack bars in Angular Material, which are notification-like elements that inform users about an operation's completion. It explains that snack bars appear briefly at the screen's bottom and do not require user interaction to disappear. The video begins by demonstrating how to import the MatSnackbarModule and integrate it into the application. It then shows how to create a button in the HTML that, when clicked, triggers the display of a snack bar with a message 'Item deleted'. The paragraph further discusses how to add an action to the snack bar, allowing users to perform an opposite action, such as 'undo'. It also touches on how to handle the snack bar's dismissal and action events using observables, providing a way to execute code when the snack bar is closed or the action is taken.
π Configuring and Customizing Angular Material Snackbars
The second paragraph delves into the configuration options available for Angular Material snack bars, such as the duration property that controls the display time before automatic dismissal. It describes how to set a duration of 2000 milliseconds (2 seconds) for the snack bar. The paragraph then moves on to discuss the 'openFromComponent' method, which allows for the creation and display of a snack bar with custom component content. An example is provided where a custom component with an orange-colored notification is created, added to the app module, and displayed when a button is clicked. The paragraph concludes by summarizing the key points about snack bars in Angular Material and encourages viewers to explore further.
Mindmap
Keywords
π‘Angular Material
π‘Snack Bars
π‘Notifications
π‘User Interaction
π‘Observables
π‘Component
π‘Duration
π‘Action
π‘Custom Content
π‘Click Event
π‘Service
Highlights
Introduction to snack bars in Angular Material as a notification system.
Snack bars are temporary notifications displayed at the bottom of the screen.
Importing the MatSnackBarModule and adding it to the material array is the first step in using snack bars.
A button with the mat-button attribute and an event handler for clicks is used to trigger a snack bar.
The openSnackBar method is defined in the component class to handle displaying snack bars.
Injecting the MatSnackBar service into the component's constructor enables the use of snack bars.
An action can be specified for the snack bar as a second argument in the open method.
The action parameter allows for a more generic openSnackBar method to handle different actions.
Observables are used to execute code when the snack bar is dismissed or an action is clicked.
The afterDismissed() and onAction() observables are subscribed to for different actions.
The duration property in the configuration object sets the automatic dismissal time for the snack bar.
The openFromComponent method creates and opens a snack bar with custom component content.
Custom components can be added to the app module's declarations and entryComponents arrays.
An example of a custom snack bar with an orange-colored notification is provided.
The video provides a comprehensive guide on implementing and customizing snack bars in Angular Material.
The use of snack bars enhances user experience by providing clear feedback on operations performed.
The video concludes with an invitation to subscribe for more content on Angular Material.
Transcripts
in this video let's take a look at snack
bars and angular material snack bars are
basically like notifications that inform
users of some operation that has been
performed they appear for a short
duration at the bottom of the screen and
they don't require user input to
disappear and at any point in time only
one snack bar notification may be
displayed let's begin by importing the
module so in material dot module dot es
import mat snack bar module and added to
the material array now in our HTML let's
add a button that will open the snack
bar so button add the mat button
attribute and handle the click event on
click we will call a method called
open snack bar passing in a string item
deleted as argument the button text is
going to be show snack bar so we now
have a button that called the open snack
bar method let's go to the class
component and define that method to open
a snack bar we need to make use of a
service and that service is mat snack
bar from angular slash material so go
ahead and import it and once you import
it inject it in the constructor
constructor private
snack bar of type mad snack bar now we
can define the open snack bar method
which will actually open the snack bar
so open snack bar accepts a message and
within the body we use the service
instance and call the open method
passing in the message so the service
instance is this dot snag bar and we
call open passing in the message
if we now save all the files and take a
look at the browser we should have a
button and when I click on the button
the snack bar should pop up at the
bottom of the screen with the message
item deleted so we have a basic snack
bar working as expected
apart from the message we can also
specify an action for a snack bar the
action is specified as the second
argument to the open method on the
service instance to keep the open snack
bar method more generic I will pass the
action parameter as well from the click
handler so in the HTML the second
parameter is going to be the string
dismiss in the component class the
method will now take a second parameter
which is action and the same is passed
to the open method
if you now take a look at the browser
click on the button a snack bar opens
and you can see the message item deleted
and the action dismiss when I click on
this action by default it closes the
snack bar a typical use case for this
type of snack bar is usually to undo an
operation for example let me rename the
button to delete
and the action to undo but when the user
does click on this action we want to
execute some code that will undo the
operation the way we are alerted when
the snack bar is dismissed or the action
is clicked is by using observables so in
the component class we are going to
start off by storing a reference to the
snack bar that has been opened select
snack bar F is equal to this dot snack
bar dot open now using this reference we
can subscribe to the after dismissed or
on action observables so the first one
is after dismissed it's a snack bar
sorry
snack bar F dot after dismissed dot
subscribe and we are going to basically
log the statement the snack bar
was dismissed similarly we also have
snack bar F dot on action dot subscribe
and over here we are going to log the
statement the slack bar action was
triggered if we now go back to the
browser and open dev tools click on the
delete button and click on the action we
can see the log statements in the
console action was clicked and the snack
bar was dismissed I just have console
log statements but you can have the code
to undo the deletion so we have the
message and the action which are passed
to the open method there is also a third
parameter which accepts some
configuration the one which would be
used mostly is the duration property the
Burien property indicates the length of
time in milliseconds to wait before
automatically dismissing the snack bar I
will add an object with the property
duration and set it to 2000 which is 2
seconds say comma and object duration
set to 2 seconds if you now go back to
the browser click on the button the
snack bar pops up but
after two seconds it automatically
dismisses and in the console you can see
the log statement so the after dismissed
part of the code was executed when the
snack bar was dismissed so when you have
an action you can use the on action
observable and when you don't have an
action but want to execute some code
when the snack bar is dismissed place
that code and the after dismissed
observable the last point to discuss is
about the open from component method as
the name indicates the open from
component method creates and opens a
snag bar with a custom component for the
content let's take a look at an example
in the existing component class which is
app component dot es I'm going to
quickly copy paste another component
now you could create the component in a
separate folder but for simplicity I
have it right here as you can see the
template is a span tag with color set to
orange now in the app module I will add
the component in the declarations array
and add to another property called entry
components
all right now that we have a component
to display as custom content let's add a
button and a click handler to open the
snack bar so in the HTML
button Matt button click handler is
going to be open custom snag bar the
text is going to be show custom slag bar
and in the component class we are going
to define open custom snack bar and this
is basically going to be this dot snag
bar dot open from component we pass in
the custom component as its first
argument and then we pass in duration of
two seconds if we now go back to the
browser and click on show custom snack
bar we should have our orange colored
notification custom snack bar all right
that is pretty much about snack bars in
angular material thank you guys for
watching feel free to subscribe I'll see
you guys in the next video
5.0 / 5 (0 votes)