ToggleButton is used to select a boolean value using a button.
import { ToggleButtonModule } from 'primeng/togglebutton';
Two-way binding to a boolean property is defined using the standard ngModel directive.
<p-toggleButton
[(ngModel)]="checked"
onLabel="On"
offLabel="Off" />
ToggleButton can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.
<form [formGroup]="formGroup">
<p-toggleButton
formControlName="checked"
onLabel="On"
offLabel="Off" />
</form>
Icons and Labels can be customized using onLabel, offLabel, onIcon and offIcon properties.
<p-toggleButton
[(ngModel)]="checked"
onLabel="Locked"
offLabel="Unlocked"
onIcon="pi pi-check"
offIcon="pi pi-times"
onIcon="pi pi-lock"
offIcon="pi pi-lock-open"
styleClass="w-9rem"
ariaLabel="Do you confirm" />
When disabled is present, the element cannot be edited and focused.
<p-toggleButton
disabled="true"
onIcon="pi pi-check"
offIcon="pi pi-times"
[(ngModel)]="checked"
onLabel="Yes"
offLabel="No"
styleClass="w-full sm:w-10rem"
ariaLabel="Confirmation" />
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-togglebutton | Container element. |
p-button-icon-left | Text element. |
p-button-icon-right | Value element. |
ToggleButton component uses an element with button role and updates aria-pressed state for screen readers. Value to describe the component can be defined with ariaLabelledBy or ariaLabel props, it is highly suggested to use either of these props as the component changes the label displayed which will result in screen readers to read different labels when the component receives focus. To prevent this, always provide an aria label that does not change related to state.
<span id="rememberme">Remember Me</span>
<p-toggleButton ariaLabelledBy="rememberme" />
<p-toggleButton ariaLabel="Remember Me" />
Key | Function |
---|---|
tab | Moves focus to the button. |
space | Toggles the checked state. |
API defines helper props, events and others for the PrimeNG ToggleButton module.
ToggleButton is used to select a boolean value using a button.
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 : ToggleButtonChangeEvent |
Defines the templates used by the component.
Defines the custom events used by the component's emitters.
Custom change event.