InputSwitchInputSwitch is used to select a boolean value.

InputSwitch - 图1

Documentation

Import

  1. import {InputSwitchModule} from 'primeng/inputswitch';
  2.  

Getting Started

Two-way binding to a boolean property is defined using the standard ngModel directive.

  1. <p-inputSwitch [(ngModel)]="checked"></p-inputSwitch>
  2.  
  1. export class ModelComponent {
  2. checked: boolean;
  3. }
  4.  

As model is two-way binding enabled, setting the bound value as true displays the state as checked by default.

  1. export class ModelComponent {
  2. checked: boolean = true;
  3. }
  4.  

Model Driven Forms

InputSwitch can be used in a model driven form as well.

  1. <p-inputSwitch formControlName="enabled"></p-inputSwitch>
  2.  

Properties

NameTypeDefaultDescription
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
tabindexnumbernullIndex of the element in tabbing order.
inputIdstringnullIdentifier of the input element.
namestringnullName of the input element.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
readonlybooleanfalseWhen present, it specifies that the component cannot be edited.

Events

NameParametersDescription
onChangeevent.originalEvent: browser event event.checked: checked state as a booleanCallback to invoke on state change.
  1. <p-inputSwitch (onChange)="handleChange($event)" [(ngModel)]="val">
  2.  
  1. export class ModelComponent {
  2. handleChange(e) {
  3. let isChecked = e.checked;
  4. }
  5. }
  6.  

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-inputswitchContainer element.
ui-inputswitch-checkedContainer element in active state.
ui-inputswitch-sliderSlider element behind the handle.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic - {{checked1}}</h3>
  2. <p-inputSwitch [(ngModel)]="checked1"></p-inputSwitch>
  3. <h3>Labels - <span> {{checked2}}</h3>
  4. <p-inputSwitch [(ngModel)]="checked2"></p-inputSwitch>
  5.  
  1. export class InputSwitchDemo {
  2. checked1: boolean = false;
  3. checked2: boolean = true;
  4. }
  5.