Open In App

How to use <mat-chip-list> and <mat-chip> in Angular Material ?

Last Updated : 08 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Angular Material is a UI component library that is developed by the Angular team to build design components for desktop and mobile web applications. In order to install it, we need to have angular installed in our project, once you have it you can enter the below command and can download it. mat-chip-list is used mainly for labels.

Installation syntax:

ng add @angular/material

Approach:

  • First, install the angular material using the above-mentioned command.
  • After completing the installation, Import ‘MatChipsModule’ from ‘@angular/material/chips’ in the app.module.ts file.
  • Then use the <mat-chip-list> tag to group all the labels or items inside this group tag.
  • Inside the <mat-chip-list> tag we need to use <mat-chip> tag for every item or labels.
  • In Angular material, we also have a chip-list type called stack chip-list where all the chips or labels are displayed vertically like a stack.
  • In order to display in such a form, we need to use this class name “mat-chip-list-stacked”.
  • If we want to change the theme then we can change it by using the color property. In angular we have 3 themes, they are primary, accent, and warn.
  • Once done with the above steps then serve or start the project.

Project Structure: It will look like the following.

Code Implementation:

app.module.ts:

Javascript




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


app.component.html:

HTML




<p>Default Chip List</p>
  
<mat-chip-list aria-label="Fish selection">
    <mat-chip color="primary" selected>
        Primary theme chip
    </mat-chip>
      
    <mat-chip color="accent" selected>
        Accent theme chip 
    </mat-chip>
      
    <mat-chip color="warn" selected>
        Warn theme chip
    </mat-chip>
      
    <mat-chip>Default theme chip </mat-chip>
</mat-chip-list>
<br><br><br>
  
<p>Stacked Chip List</p>
  
<mat-chip-list class="mat-chip-list-stacked"
    aria-label="Fish selection">
      
    <mat-chip color="primary" selected>
        Primary theme chip 
    </mat-chip>
      
    <mat-chip color="accent" selected>
        Accent theme chip
    </mat-chip>
      
    <mat-chip color="warn" selected>
        Warn theme chip
    </mat-chip>
      
    <mat-chip>Default theme chip </mat-chip>
</mat-chip-list>


Output:



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

Similar Reads