Change switch state using toggle method in Angular Switch component

27 Apr 20241 minute to read

This section explains about how to toggle between the switch states using toggle method.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SwitchModule } from '@syncfusion/ej2-angular-buttons'



import { Component, ViewChild } from '@angular/core';
import { SwitchComponent } from '@syncfusion/ej2-angular-buttons';

@Component({
imports: [
        
        SwitchModule
    ],


standalone: true,
    selector: 'app-root',
    template: `<div class="e-section-control">
                <!-- To Render Switch. -->
               <ejs-switch #switch onLabel="ON" offLabel="OFF" (created)='created()'></ejs-switch>
               </div>`
})

export class AppComponent {
    @ViewChild('switch')
    public switch: SwitchComponent| any;
    public created() {
        this.switch.toggle();
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Switch triggers change event on every state stage to perform custom operations.