ToggleButtonToggleButton is used to select a boolean value using a button.

ToggleButton - 图1

Documentation

Import

  1. import {ToggleButtonModule} from 'primeng/togglebutton';
  2.  

Getting Started

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

  1. <p-toggleButton [(ngModel)]="checked"></p-toggleButton>
  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.

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

Model Driven Forms

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

  1. <p-toggleButton formControlName="agreed"></p-toggleButton>
  2.  

Labels

Labels are customized using onLabel and offLabel properties.

  1. <p-toggleButton onLabel="I confirm" offLabel="I reject"
  2. onIcon="pi pi-check" offIcon="pi pi-times" [(ngModel)]="val"></p-toggleButton>
  3.  

Icons

Icon on a ToggleButton is specified with the onIcon and offIcon properties and position is customized using the iconPos property.

  1. <p-toggleButton onLabel="I confirm" offLabel="I reject"
  2. onIcon="fa fa-check" offIcon="fa fa-times" iconPos="right" [(ngModel)]="val"></p-toggleButton>
  3.  

Properties

NameTypeDefaultDescription
onLabelstringYesLabel for the on state.
offLabelstringNoLabel for the off state.
onIconstringnullIcon for the on state.
offIconstringnullIcon for the off state.
iconPosstringleftPosition of the icon, valid values are "left" and "right".
stylestringnullInline style of the element.
styleClassstringnullStyle class of the element.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
tabindexnumbernullIndex of the element in tabbing order.
inputIdstringnullIdentifier of the focus input to match a label defined for the component.

Events

NameParametersDescription
onChangeevent.originalEvent: browser event event.checked: boolean value to represent checked state.Callback to invoke on state change.
  1. <p-toggleButton (onChange)="handleChange($event)" [(ngModel)]="val">
  2.  
  1. export class ModelComponent {
  2. handleChange(e) {
  3. var isChecked = e.checked;
  4. }
  5. }
  6.  

Styling

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

NameElement
ui-togglebuttonContainer element
ui-button-icon-leftIcon element.
ui-button-icon-rightIcon element.
ui-button-textLabel element.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic - ({{checked1}})</h3>
  2. <p-toggleButton [(ngModel)]="checked1" [style]="{'width':'150px'}"></p-toggleButton>
  3. <h3>Customized - ({{checked2}})</h3>
  4. <p-toggleButton [(ngModel)]="checked2" onLabel="I confirm" offLabel="I reject" onIcon="pi pi-check" offIcon="pi pi-times" [style]="{'width':'150px'}"></p-toggleButton>
  5.  
  1. export class ToggleButtonDemo {
  2. checked1: boolean = false;
  3. checked2: boolean = true;
  4. }
  5.