Snackbar In Angular Using Ignite-ui-angular

The Ignite UI for Angular Snack Bar component provides feedback about an operation with a single-line message, which can include a link to an action such as Undo. The Snack Bar message appears above all other screen elements, located at the bottom of a mobile device screen or at the lower left of larger device screens.

Web and mobile apps often require a confirmation of an activity or operation and there are several ways to achieve this. A traditional method of a confirmation is to show a popup message or a text message. However, today’s browsers, devices, and platforms can provide modern and creative ways to notify a user about the operation. Angular Snack Bar is one of those controls.

The Ignite UI for Angular Snack Bar component provides feedback about an operation with a single-line message, which can include a link to an action such as Undo. The Snack Bar message appears above all other screen elements, located at the bottom of a mobile device screen or at the lower left of larger device screens.

This article is an introduction to Angular Snack Bar component and how to use it in a working demo app.

Let's get started.

Note
If you do not have IgniteUI for Angular installed, check out Ignite UI for Angular, download, and install it.

Once you’re all set with Ignite UI for Angular, open VS Code or your favorite code editor and create a new project.

In my case, I use VS Code and create a new project:

“ng new SnackbarDemo”

Once the project is created, open app.module.ts and add the following code:

  1. import { IgxSnackbarModule } from 'igniteui-angular';  
  2. import { BrowserAnimationsModule } from '@angular/platform-browser/animations';  
  3. imports : [  
  4.    BrowserModule,  
  5.    BrowserAnimationsModule,  
  6.    IgxSnackbarModule  

 

The above code imports IgniteUI Snackbar Module.

For this demo project, we will not create a separate component for Snackbar, instead, we are going to code in app.component.

Next go to the app.component.html and paste the following code. This code adds a button and a Snackbar component to the page.

  1. <div style="margin: 20px">  
  2.     <button igxButton="raised" class="btn btn-success" (click)="snackbar.show()">Show     SnackBar</button>  
  3.   
  4.     <div style="width: 30%; margin-top: 10px">  
  5.         <igx-snackbar #snackbar message="Snackbar show"></igx-snackbar>  
  6.     </div>  
  7. </div>  

Listing 3

Now, go to the VS Code terminal and run the following command.

“ng serve --o”

The final output will be like the following figure. That’s the default Snackbar component.

Snackbar In Angular  

Snackbar with manually close button

The Snackbar message appears and disappears after some time. The default display time is 4 sec. We can customize this behavior by setting its property “Autohide” to false. To do this go to the app.component.html file and paste the following code.

  1. <button igxButton="raised" (click)="snackbar.show()">Show Snackbar</button>  
  2. <div>  
  3.     <igx-snackbar #snackbar message="Snackbar shows. Click close button to close the snackbar"   [autoHide]="false" actionText="CLOSE" (onAction)="close(snackbar)"></igx-snackbar>  
  4. </div>  

The above code sets the autoHide property to false.

See the onAction method we define in snackbar. Now we need to create an action in the app.component.ts file. This method simply takes the snackbar as a variable and calls the hide function.

  1. public close(element) {  
  2.     element.hide();  
  3.   }  

Now, the new UI looks like the following. As you can see from this below figure, the CLOSE button appears and clicking on it will hide the control.

Snackbar In Angular  

Snack bar visible duration

We can also customize the display time of a Snackbar by setting its property, displayTime. The display time takes a number of milliseconds. The following code snippet will keep a Snackbar open for half a second.

  1. <igx-snackbar #snackbar message="Custom time to show Snackbar" displayTime="500">  
  2. </igx-snackbar> 

Use Snackbar in a list

One of the common uses of a Snackbar is to use it in a list of items. When an aciton occurs on an item such as item added, updated, or deleted in the list, you want to notify the user.

Add the following code to the app.component.html file. In this code, we create a table with 3 columns. The data is binding dynamically using Angular for the loop.

  1. <div style="margin: 20px">  
  2.     <table class="table table-bordered" style="width: 30%">  
  3.         <tr>  
  4.             <th>  
  5.                 Id.  
  6.             </th>  
  7.             <th>  
  8.                 Name  
  9.             </th>  
  10.             <th>  
  11.                 Send Notification  
  12.             </th>  
  13.         </tr>  
  14.         <tr *ngFor="let obj of data">  
  15.             <td>  
  16.                 {{obj.Id}}  
  17.             </td>  
  18.             <td>  
  19.                 {{obj.Name}}  
  20.             </td>  
  21.             <td>  
  22.                 <button class="btn btn-success" (click)="SendNotificationClick(snackbar,                         obj.Name)">Send Notification</button>  
  23.             </td>  
  24.         </tr>  
  25.     </table>  
  26.   
  27.     <div style="width: 30%; margin-top: 5px">  
  28.         <igx-snackbar #snackbar></igx-snackbar>  
  29.     </div>  
  30. </div>  

Now, go to the app.component.ts file and paste the code. This code sends a notification on an item click in the list.

  1. export class AppComponent {  
  2.   title = 'app';  
  3.   data: any[];  
  4.   
  5.   constructor() {  
  6.     this.data = [  
  7.       {  
  8.         'Id': 1,  
  9.         'Name''Nikki'  
  10.       },  
  11.       {  
  12.         'Id': 2,  
  13.         'Name''June'  
  14.       },  
  15.       {  
  16.         'Id': 3,  
  17.         'Name''Stephen'  
  18.       },  
  19.       {  
  20.         'Id': 4,  
  21.         'Name''Jhonny'  
  22.       },  
  23.       {  
  24.         'Id': 5,  
  25.         'Name''Shri'  
  26.       }  
  27.     ];  
  28.   }  
  29.   
  30.   public SendNotificationClick(element: any, name: string): void {  
  31.     //Notification logic goes here and chech if successfully send  
  32.     element.message = 'Notification send to ' + name + ' successfully';  
  33.     element.show();  
  34.   }  
  35. }  

If you save and run the app the new output looks like the following.

Snackbar In Angular  

Now, when you click on the Send Notification button, you will see the notification is displayed at the bottom. You can use the same control for notifying the user on a Grid, Table and other controls.

Conclusion

The Snackbar component of Ignite UI is very easy to implement in any Angular project to show a pop message. Snackbar also allows you to customize the CSS so that you can change it according to your project theme.

Learn more, download a free trial, and get started here,

Get Started with Ignite UI for Angular.