---
title: "Angular Forms Switch Example | Mobiscroll"
component: "Forms"
framework: "Angular"
library_version: "6.1.5"
url: "https://demo.mobiscroll.com/angular/forms/switch"
---

# Switch - Angular Forms demo

Angular switch/toggle demo with or without description. Alternative to the checkbox. Supports disabled styling.

- [Live demo](https://demo.mobiscroll.com/angular/forms/switch)
- [View code on GitHub](https://github.com/acidb/mobiscroll-demos-angular/tree/main/src/demos/form-components/forms/switch)
- This demo in other frameworks: [JavaScript](https://demo.mobiscroll.com/javascript/forms/switch.md), [jQuery](https://demo.mobiscroll.com/jquery/forms/switch.md), [React](https://demo.mobiscroll.com/react/forms/switch.md), [Vue](https://demo.mobiscroll.com/vue/forms/switch.md)

## Description

Use the switch for turning options and setting fields on/off. Different color presets are available.

## What this demo shows

- This demo shows examples for the switch component.
- **Switch states** Switch controls displayed in both on and off states.
- **Labels and descriptions** Switches paired with titles and descriptive text.
- **Disabled states** Disabled switches displayed in both on and off states.
- **Color presets** Switches using the primary, secondary, success, danger, warning, and info color presets.

## Best for

- **Settings and preferences** Let users enable or disable configurable options.
- **Feature controls** Turn individual features or modes on and off.
- **Notification preferences** Manage binary choices for notification categories such as email, push, or reminders.
- **Privacy and permissions** Present clear on/off choices for consent, visibility, or access settings.

## Tutorial

```html
<mbsc-switch label="Switch Off" description="This is description for Switch" [(ngModel)]="sw1"></mbsc-switch>
<mbsc-switch label="Switch On" description="This is description for Switch 2" [(ngModel)]="sw2"></mbsc-switch>
<mbsc-switch label="Disabled Switch Off" [(ngModel)]="sw3" [disabled]="true"></mbsc-switch>
<mbsc-switch label="Disabled Switch On" [(ngModel)]="sw4" [disabled]="true"></mbsc-switch>
```

```html
<mbsc-switch label="Primary" color="primary" [(ngModel)]="primary"></mbsc-switch>
<mbsc-switch label="Secondary" color="secondary" [(ngModel)]="secondary"></mbsc-switch>
<mbsc-switch label="Success" color="success" [(ngModel)]="success"></mbsc-switch>
<mbsc-switch label="Danger" color="danger" [(ngModel)]="danger"></mbsc-switch>
<mbsc-switch label="Warning" color="warning" [(ngModel)]="warning"></mbsc-switch>
<mbsc-switch label="Info" color="info" [(ngModel)]="info"></mbsc-switch>
```

## Code

### Component

```ts
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MbscModule, setOptions } from '@mobiscroll/angular';

setOptions({
  theme: 'ios',
  themeVariant: 'light'
});

@Component({
  selector: 'app-root', 
  templateUrl: './app.component.html',
  standalone: true,
  imports: [CommonModule, FormsModule, MbscModule],
})
export class AppComponent {
  sw1 = false;
  sw2 = true;
  sw3 = false;
  sw4 = true;
  primary = true;
  secondary = true;
  success = true;
  danger = true;
  warning = true;
  info = true;
}
```

### Template

```html
<mbsc-page>
  <div class="mbsc-form-group">
    <div class="mbsc-form-group-title">Switch</div>
    <mbsc-switch label="Switch Off" description="This is description for Switch" [(ngModel)]="sw1"></mbsc-switch>
    <mbsc-switch label="Switch On" description="This is description for Switch 2" [(ngModel)]="sw2"></mbsc-switch>
    <mbsc-switch label="Disabled Switch Off" [(ngModel)]="sw3" [disabled]="true"></mbsc-switch>
    <mbsc-switch label="Disabled Switch On" [(ngModel)]="sw4" [disabled]="true"> </mbsc-switch>
  </div>
  <div class="mbsc-form-group">
    <div class="mbsc-form-group-title">Switch colors</div>
    <mbsc-switch label="Primary" color="primary" [(ngModel)]="primary"></mbsc-switch>
    <mbsc-switch label="Secondary" color="secondary" [(ngModel)]="secondary"></mbsc-switch>
    <mbsc-switch label="Success" color="success" [(ngModel)]="success"></mbsc-switch>
    <mbsc-switch label="Danger" color="danger" [(ngModel)]="danger"></mbsc-switch>
    <mbsc-switch label="Warning" color="warning" [(ngModel)]="warning"></mbsc-switch>
    <mbsc-switch label="Info" color="info" [(ngModel)]="info"></mbsc-switch>
  </div>
</mbsc-page>
```

## Implementation notes

- The on/off state is controlled by `checked`/`defaultChecked` (React `defaultChecked={true}`; JS/jQuery `checked` attribute on `<input type="checkbox" mbsc-switch>`; Angular/Vue bind it through two-way binding - see below).
- `label` sets the switch's title text (JS/jQuery: `data-label` attribute).
- `description` sets secondary helper text shown under the label (JS/jQuery: `data-description` attribute).
- `disabled` renders the switch non-interactive in either the on or off state; combine with `checked`/`defaultChecked` to show a disabled-on switch.
- `color` applies a preset accent color to the track/thumb when on: `'primary'`, `'secondary'`, `'success'`, `'danger'`, `'warning'`, or `'info'`.
- Framework state binding differs: JS/jQuery both read/write state imperatively via the shared `mobiscroll.getInst(element).checked` helper; jQuery additionally offers the chained form `$(element).mobiscroll('setVal', ...)` (not available in plain JS); React uses `defaultChecked`/`checked` + `onChange`; Angular binds via `[(ngModel)]="value"` for two-way sync; Vue binds via `v-model="value"`.
- Framework markup differences: JS/jQuery wraps a plain `<input type="checkbox" mbsc-switch>` in a `<label>` with `data-*` attributes for `label`/`description`/`color`; React uses `<Switch label="..." description="..." color="primary" disabled={true} defaultChecked={true} />`; Angular uses `<mbsc-switch label="..." color="primary" [(ngModel)]="value" [disabled]="true">`; Vue uses `<MbscSwitch v-model="value" label="..." color="primary" />` (with `disabled` set separately per switch since it isn't part of the `v-model` binding).
- Grouping switches under a shared `mbsc-form-group` with an `mbsc-form-group-title` is a layout convention for the demo, not a switch option - each switch instance is independently configured.

## Run it in your project

1. If not signed up yet, or don't own a license, you can [start a free trial](https://mobiscroll.com/starttrial).

2. Clone the repository:

   ```
   git clone https://github.com/acidb/mobiscroll-demos-angular.git
   ```

3. If not installed yet, install the Mobiscroll CLI from NPM:

   ```
   npm install -g @mobiscroll/cli
   ```

4. Config Mobiscroll - open a terminal window in the root directory and run:

   ```
   mobiscroll config angular
   ```

5. Install dependencies - open a terminal window in the root directory and run:

   ```
   npm install
   ```

6. Run the project:

   ```
   ng serve
   ```

On a trial, add `--trial` to the config command: `mobiscroll config angular --trial`.

Running a demo locally requires a Mobiscroll trial or a license; setup is per framework at https://mobiscroll.com/docs/angular/getting-started/installation

## Related demos

- Previous: [Checkbox](https://demo.mobiscroll.com/angular/forms/checkbox.md) - Angular checkbox and checklist demo and styling with or without description text. Supports disabled styling.
- Next: [Radio buttons](https://demo.mobiscroll.com/angular/forms/radio-button.md) - Angular radio button demo for single select option lists. Use it with or without description, supports disabled styling.
