InputSwitch is used to select a boolean value.
import { InputSwitchModule } from 'primeng/inputswitch';
Two-way value binding is defined using ngModel.
<p-inputSwitch [(ngModel)]="checked" />
InputSwitch can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.
<p-inputSwitch formControlName="checked" />
Enabling ngModel property displays the component as active initially.
<p-inputSwitch [(ngModel)]="checked" />
Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.
<p-inputSwitch [(ngModel)]="checked" class="ng-dirty ng-invalid" />
When disabled is present, the element cannot be edited and focused.
<p-inputSwitch [(ngModel)]="checked" [disabled]="true" />
Name | Element |
---|---|
p-inputswitch | Container element. |
p-inputswitch-checked | Container element in active state. |
p-inputswitch-slider | Slider element behind the handle. |
InputSwitch component uses a hidden native checkbox element with switch role internally that is only visible to screen readers. Value to describe the component can either be provided via label tag combined with inputId prop or using ariaLabelledBy, ariaLabel props.
<label for="switch1">Remember Me</label>
<p-inputSwitch inputId="switch1" />
<span id="switch2">Remember Me</span>
<p-inputSwitch ariaLabelledBy="switch2" />
<p-inputSwitch ariaLabel="Remember Me" />
Key | Function |
---|---|
tab | Moves focus to the switch. |
space | Toggles the checked state. |
API defines helper props, events and others for the PrimeNG InputSwitch module.
InputSwitch is used to select a boolean value.
Defines the input properties of the component.
Defines emit that determine the behavior of the component based on a given condition or report the actions that the component takes.
name | parameters | description | |
---|---|---|---|
onChange | event : InputSwitchChangeEvent | Callback to invoke when the on value change. |
Defines the custom events used by the component's emitters.
Custom change event.