Angular material tutorial with examples using frequently used classes

 

Angular Material Tutorial: Frequently Used Classes and Components

Angular Material is a UI component library that provides modern, responsive, and accessible components to enhance the look and feel of your Angular applications. In this tutorial, we'll explore the most commonly used Angular Material components, including examples of how to use them with frequently used classes.


1. Setting Up Angular Material

Before you can start using Angular Material components, you need to install and configure Angular Material in your Angular project.

Step 1: Install Angular Material

First, if you haven't already, create an Angular project by running the following command:

bash
ng new my-angular-app cd my-angular-app

Next, install Angular Material:

bash
ng add @angular/material

This command will prompt you to choose a theme and whether you want to set up global typography and animations. Select the default options.

Step 2: Import Angular Material Modules

To use Angular Material components, import the required modules into your app's main module (app.module.ts).

For example, let's import some commonly used Angular Material modules:

typescript
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; // Import Angular Material modules import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; import { MatInputModule } from '@angular/material/input'; import { MatToolbarModule } from '@angular/material/toolbar'; import { MatIconModule } from '@angular/material/icon'; import { MatSnackBarModule } from '@angular/material/snack-bar'; import { MatDialogModule } from '@angular/material/dialog'; @NgModule({ declarations: [AppComponent], imports: [ BrowserModule, MatButtonModule, MatCardModule, MatInputModule, MatToolbarModule, MatIconModule, MatSnackBarModule, MatDialogModule, ], providers: [], bootstrap: [AppComponent], }) export class AppModule {}

2. Frequently Used Angular Material Components

Here are some of the most commonly used Angular Material components with examples:

2.1 MatButton (Button)

The MatButton component is used to create material-styled buttons with various types like flat, raised, and stroked.

html
<!-- Flat Button --> <button mat-button>Flat Button</button> <!-- Raised Button --> <button mat-raised-button color="primary">Primary Raised Button</button> <!-- Stroked Button --> <button mat-stroked-button color="accent">Stroked Button</button> <!-- Icon Button --> <button mat-icon-button> <mat-icon>home</mat-icon> </button>

2.2 MatCard (Card)

The MatCard component is used to create card-based layouts with images, titles, and content.

html
<mat-card class="example-card"> <mat-card-header> <mat-card-title>Card Title</mat-card-title> <mat-card-subtitle>Card Subtitle</mat-card-subtitle> </mat-card-header> <img mat-card-image src="https://via.placeholder.com/150" alt="Card image"> <mat-card-content> <p>This is some content inside a card.</p> </mat-card-content> <mat-card-actions> <button mat-button>Like</button> <button mat-button>Share</button> </mat-card-actions> </mat-card>

2.3 MatInput (Input Field)

The MatInput component is used for text fields. It supports form validation and styling.

html
<mat-form-field appearance="fill"> <mat-label>Enter your name</mat-label> <input matInput placeholder="Name"> </mat-form-field> <mat-form-field appearance="outline"> <mat-label>Enter your email</mat-label> <input matInput type="email" placeholder="Email"> </mat-form-field>

2.4 MatToolbar (Toolbar)

The MatToolbar component is a top navigation bar that is commonly used for displaying titles, navigation icons, and action buttons.

html
<mat-toolbar color="primary"> <mat-icon>menu</mat-icon> <span>My Application</span> <span class="spacer"></span> <button mat-button>Login</button> </mat-toolbar>

In your styles, you can define the .spacer class to push the login button to the right:

css
.spacer { flex: 1 1 auto; }

2.5 MatIcon (Icon)

MatIcon is used to display Material icons in your application. You need to import MatIconModule to use this component.

html
<button mat-icon-button> <mat-icon>search</mat-icon> </button> <mat-icon>home</mat-icon>

You can use icons from the Material Design Icons library, which comes pre-configured with Angular Material.

2.6 MatSnackBar (SnackBar)

The MatSnackBar component is used to show temporary messages, like notifications or alerts, that appear at the bottom of the screen.

html
<button mat-raised-button (click)="openSnackBar()">Show Notification</button>

In your component (app.component.ts):

typescript
import { MatSnackBar } from '@angular/material/snack-bar'; export class AppComponent { constructor(private snackBar: MatSnackBar) {} openSnackBar() { this.snackBar.open('This is a snack bar message!', 'Close', { duration: 2000, }); } }

2.7 MatDialog (Dialog)

MatDialog is used to display modal dialogs (pop-ups).

html
<button mat-raised-button (click)="openDialog()">Open Dialog</button>

In your component (app.component.ts):

typescript
import { MatDialog } from '@angular/material/dialog'; import { DialogContentComponent } from './dialog-content/dialog-content.component'; @Component({ selector: 'app-root', templateUrl: './app.component.html', }) export class AppComponent { constructor(private dialog: MatDialog) {} openDialog() { this.dialog.open(DialogContentComponent); } }

In the dialog content component (dialog-content.component.ts):

html
<h1 mat-dialog-title>Dialog Title</h1> <div mat-dialog-content> <p>This is the content of the dialog.</p> </div> <div mat-dialog-actions> <button mat-button mat-dialog-close>Close</button> </div>

You will need to declare the dialog content component in the app.module.ts.

typescript
import { DialogContentComponent } from './dialog-content/dialog-content.component'; @NgModule({ declarations: [DialogContentComponent], entryComponents: [DialogContentComponent], imports: [MatDialogModule], })

3. Styling and Customization

3.1 Customizing Colors

You can change the color scheme of components using the color attribute. Angular Material comes with predefined color palettes like primary, accent, and warn.

html
<button mat-raised-button color="primary">Primary Button</button> <button mat-raised-button color="accent">Accent Button</button> <button mat-raised-button color="warn">Warn Button</button>

3.2 Using Custom Classes

You can also apply custom classes to style the Angular Material components:

html
<mat-card class="custom-card"> <mat-card-header> <mat-card-title>Custom Styled Card</mat-card-title> </mat-card-header> </mat-card>

In your CSS file:

css
.custom-card { background-color: #f8f9fa; border-radius: 10px; }

4. Conclusion

This tutorial covered some of the most commonly used Angular Material components and their frequently used classes, including MatButton, MatCard, MatInput, MatSnackBar, MatDialog, and MatToolbar. These components provide a rich set of UI elements that can be easily integrated into your Angular application, helping you create modern, responsive, and user-friendly interfaces.

To explore more components and features, visit the official Angular Material documentation.

Comments

Popular posts from this blog

PrimeNG tutorial with examples using frequently used classes

Docker and Kubernetes Tutorials and QnA

Building strong foundational knowledge in frontend development topics