Open In App

mat-slide-toggle in Angular material

Last Updated : 27 Jan, 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-slide-toggle is a kind of switch or button that is used to toggle between states like one and off or true or false.

Installation syntax:

ng add @angular/material

Approach:

  • First, install the angular material using the above-mentioned command.
  • After completing the installation, Import ‘MatSlideToggleModule’ from ‘@angular/material/slide-toggle’ in the app.module.ts file.
  • Then use the mat-slide-toggle tag to display a toggle button.
  • And in order to write the significance of that button then we need to mention it between the opening and closing tags.
  • 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.

Code Implementation:

app.module.ts:

Javascript




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


app.component.html:

HTML




<mat-slide-toggle color="primary">
  Slide me to get the primary theme!
</mat-slide-toggle> <br />
<br />
<mat-slide-toggle color="accent">
  Slide me to get the accent theme!
</mat-slide-toggle>
<br />
<br />
<mat-slide-toggle color="warn">
  Slide me to get the warn theme!
</mat-slide-toggle>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads