Open In App

Angular Material Checkbox

Last Updated : 04 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Angular Material library is a UI component library created by the Angular team to create design components for desktop and mobile web applications. To install it, we’ll need to have Angular installed in our project. After that’s done, use the command below to download it. Checkbox is used when we need to select one or more options from multiple options given to us. The Angular Directive <mat-checkbox> is used to provide an upgraded checkbox with material design styling and animation features. The settings required to draw a checkbox control using Angular Material will be demonstrated in this article.

 

Syntax:

<mat-checkbox></mat-checkbox>

Note: <mat-checkbox> provides the same functionality as a native, <input type=”checkbox”> enhanced with Material Design styling and animations.

Installation syntax:

The most basic requirement is that Angular CLI be installed on the system in order to add and configure the Angular material library. After satisfying the required condition, we can use the Angular CLI to run the following command:

ng add @angular/material

Refer to this article for detailed information about installation and setup.

Adding check box component:

‘MatCheckboxModule’ must be imported from ‘@angular/material/checkbox’ in the app.module.ts file of our app.  

import { MatCheckboxModule } from '@angular/material/checkbox';

Steps in order:

  • Use the above-mentioned command to install the angular material.
  • Import ‘MatCheckboxModule’ from ‘@angular/material/checkbox’ in the app.module.ts file after finishing the installation.
  • We can use progress bars by just using the < mat-checkbox> tag.
  • We need to give the tag a value attribute in order to indicate the progress.
  • After you’ve completed the preceding stages, you can either serve or begin the project.

checkbox: The <mat-checkbox> element has the checkbox label as its content. Setting the ‘labelPosition’ property to ‘before’ or ‘after’ will place the label before or after the checkbox.

Indeterminate checkbox: Similarly to the native < input type=”checkbox”>, mat-checkbox> allows an indeterminate state. The checkbox will appear as indeterminate regardless of the checked value if the indeterminate attribute is true. The uncertain state will be removed if the user interacts with the checkbox.

disabled checkbox: user cannot use this checkbox. The disabled attribute determines whether a checkbox should be disabled or not. A deactivated element is not clickable or functional. Browsers commonly portray disabled components in gray by default.

checkboxes can also have different themes or colors which are primary, accent and warm.

Let’s look at an example where we implement all the above cases.

Example:

app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { BrowserAnimationsModule } from 
    '@angular/platform-browser/animations';
import { FormsModule } from '@angular/forms';
  
@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    BrowserAnimationsModule,
    MatCheckboxModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}


app.component.html




<h1 class="title">GeeksforGeeks</h1>
<h3>checkbox:</h3>
<mat-checkbox layout-align="center center">
  i'm a normal checkbox
</mat-checkbox>
<h3>Indeterminate check box:</h3>
<mat-checkbox indeterminate="true">
  i'm an Indeterminate checkbox
</mat-checkbox>
<h3>disabled checkbox:</h3>
<mat-checkbox disabled> i'm a disabled checkbox </mat-checkbox>
<h3>Check box with different themes:</h3>
<mat-checkbox color="primary"> primary theme</mat-checkbox>
<br />
<mat-checkbox color="accent"> accent theme</mat-checkbox>
<br />
<mat-checkbox color="warn"> warn theme</mat-checkbox>
<br />


app.component.css




.title {
  color: green;
  display: flex;
  justify-content: center;
  align-items: center;
}


Output:

 

Reference: https://material.angular.io/components/checkbox/api



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads