Open In App

Angular MDBootstrap Switch Component

MDBootstrap is a Material Design and bootstrap-based Angular UI library that is used to make attractive webpages with its seamless and easy-to-use component. In this article, we will see how to use Switch Component in Angular MDBootstrap.

The Switch Component is used to indicate the on/off state of the component. The switch can be utilized to perform the On/Off functionality or provide the preference to choose one of two predefined options.



Syntax:

<div class="custom-control custom-switch">
  <input type="checkbox" class="custom-control-input">
  <label class="custom-control-label">Switch</label>
</div>

Approach:



npm install
cd appname
ng serve

Project Structure: After complete installation, it will look like the following:

Project Structure

Example 1: This is the basic example that illustrates how to use the Switch component in Angular MDBootstrap. Here, we have implemented the default & the checked switch.




<div id="gfg">
    <h1>GeeksforGeeks</h1>
    <h4>
        Angular MDBootstrap Switch Component
    </h4>
    <br />
    <div class="custom-control custom-switch">
        <input type="checkbox" 
               class="custom-control-input" 
               id="customSwitches">
        <label class="custom-control-label" 
               for="customSwitches">
            Switch
        </label>
    </div>
    <br/>
    <div class="custom-control custom-switch">
        <input type="checkbox" 
               class="custom-control-input" 
               id="gfg1" 
               checked>
        <label class="custom-control-label" 
               for="gfg1">
            Switch
        </label>
    </div>
</div>




import { AfterViewInit, Component, ViewChildren } from "@angular/core";
  
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"],
})
export class AppComponent implements AfterViewInit {
  @ViewChildren(AppComponent) collapses!: AppComponent[];
  
  ngAfterViewInit(): void {}
  togglevisible = false;
  
  togglebg() {
    this.togglevisible = !this.togglevisible;
  }
}




import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
  
import { AppComponent } from "./app.component";
import { MDBBootstrapModule } from "angular-bootstrap-md";
import { FormsModule } from "@angular/forms";
  
@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MDBBootstrapModule.forRoot(),
    FormsModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Output:

 

Example 2: In this example, we will know how to disable the Switch component in Angular MDBootstrap.




<div id="gfg">
    <h1>GeeksforGeeks</h1>
    <h4>
        Angular MDBootstrap Switch Component
    </h4>
    <br />
    <div class="custom-control custom-switch">
        <input type="checkbox" 
               class="custom-control-input" 
               id="gfg1" disabled>
        <label class="custom-control-label" 
               for="gfg1">
            Switch
        </label>
    </div>
</div>




import { AfterViewInit, Component, ViewChildren } from "@angular/core";
  
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"],
})
export class AppComponent implements AfterViewInit {
  @ViewChildren(AppComponent) collapses!: AppComponent[];
  
  ngAfterViewInit(): void {}
  togglevisible = false;
  
  togglebg() {
    this.togglevisible = !this.togglevisible;
  }
}




import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
  
import { AppComponent } from "./app.component";
import { MDBBootstrapModule } from "angular-bootstrap-md";
import { FormsModule } from "@angular/forms";
  
@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MDBBootstrapModule.forRoot(),
    FormsModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Output:

 

Reference: https://mdbootstrap.com/docs/angular/forms/switch


Article Tags :