List Operation In Angular Material

Today, we are going to see how we can use the List component in Angular Material

Let’s Start

Step1: Create a New Angular Application

ng new demo

Step2: Now, install Angular Material

ng add @angular/material

Step3: import module in app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatListModule } from '@angular/material/list';     //import matlistmodule
import { FormsModule } from '@angular/forms';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatListModule,            //add MatListModule
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent],
  schemas: [
    CUSTOM_ELEMENTS_SCHEMA
]
})
export class AppModule { }

Step4: write below code in app.component.ts

import { Component } from '@angular/core';

export interface Section 
{
  name: string;
  updated: Date;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent 
{
  notes:Section[] = [{
       name:'Add New Employee',
       updated: new Date('2/20/16'),
  },
  {
       name:'Add New DepartMent',
       updated: new Date('3/20/16'),
  }]
}

Step5: write below code in app.component.html

<mat-list role="list">
  <mat-list-item role="listitem">Item 1</mat-list-item>
  <mat-list-item role="listitem">Item 2</mat-list-item>
  <mat-list-item role="listitem">Item 3</mat-list-item>
  <div mat-subheader>Notes</div>
  <mat-list-item *ngFor="let note of notes">
     <mat-icon mat-list-icon>note</mat-icon>
     <div mat-line>{{note.name}}</div>
     <div mat-line>{{note.updated | date}}</div>
  </mat-list-item>
</mat-list>


Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories