Angular PrimeNG ToggleButton Component - GeeksforGeeks

22 Aug, 2021
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the ToggleButton component in Angular PrimeNG.
ToggleButton component is used to make a button that users can toggle by clicking on it. 
Properties:
onLabel: It is used to set the label for the on-state. It is of string data type, the default value is null.
offLabel: It is used to set the label for the off state. It is of string data type, the default value is null.
onIcon: It is used to set the icon for the on-state. It is of string data type, the default value is null.
offIcon: It is used to set the icon for the off state. It is of string data type, the default value is null.
iconPos: It is used to set the position of the icon, valid values are “left” and “right”. It is of string data type, the default value is left.
style: It is used to set the inline style of the element. It is of string data type, the default value is null.
styleClass: It is used to set the style class of the element. It is of string data type, the default value is null.
disabled: It specifies that the element should be disabled. It is of a boolean data type, the default value is false.
tabindex: It is used to set the Index of the element in tabbing order. It is of number data type, the default value is null.
inputId:  It is an ID identifier of the underlying input element. It is of string data type, the default value is null
ariaLabel: It is used to define a string that labels the input element, It is of string data type, the default value is null.
Event:
onChange: it is a callback that is fired on state change.
 
Styling:
p-togglebutton: It is a styling Container element
p-button-icon-left: It is a styling Icon element.
p-button-icon-right: It is a styling Icon element.
p-button-text: It is a styling Label element.
Creating Angular Application & module installation:
Step 1: Create an Angular application using the following command.ng new appname
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.cd appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Step 3: Install PrimeNG in your given directory.npm install primeng --save
npm install primeicons --save
Step 3: Install PrimeNG in your given directory.
npm install primeng --save
npm install primeicons --save
Project Structure: It will look like the following.
 
Example 1: This is the basic example that shows how to use the ToggleButton component.
app.component.html
<h2>GeeksforGeeks</h2><h5>PrimeNG ToggleButton Component</h5><p-toggleButton  [(ngModel)]="checked1"  onIcon="pi pi-caret-left"  offIcon="pi pi-caret-right"  onLabel="ToggleButton Component"  offLabel="Enabled Button"></p-toggleButton>  <p-toggleButton  [(ngModel)]="checked2"  onIcon="pi pi-caret-left"  offIcon="pi pi-caret-right"  [disabled]  onLabel="ToggleButton Component"  offLabel="Disabled Button"></p-toggleButton>
app.component.ts
import { Component } from "@angular/core";import { SelectItem, PrimeNGConfig } from "primeng/api";  @Component({  selector: "my-app",  templateUrl: "./app.component.html",})export class AppComponent {  constructor(private primengConfig: PrimeNGConfig) {}    ngOnInit() {    this.primengConfig.ripple = true;  }    checked1: boolean = false;    checked2: boolean = false;}
app.module.ts
import { NgModule } from "@angular/core";import { BrowserModule } from "@angular/platform-browser";import { FormsModule } from "@angular/forms";import { BrowserAnimationsModule }     from "@angular/platform-browser/animations";  import { AppComponent } from "./app.component";  import { ToggleButtonModule } from "primeng/togglebutton";  @NgModule({  imports: [    BrowserModule,    BrowserAnimationsModule,    ToggleButtonModule,    FormsModule,  ],  declarations: [AppComponent],  bootstrap: [AppComponent],})export class AppModule {}
Output:
Example 2: In this example, we will know how to use iconPos property in the toggleButton component. 
app.component.html
<h2>GeeksforGeeks</h2><h5>PrimeNG ToggleButton Component</h5><p-toggleButton  [(ngModel)]="checked1"  onIcon="pi pi-caret-left"  offIcon="pi pi-caret-right"  onLabel="ToggleButton Component"  offLabel="GeeksforGeeks"  iconPos="right"></p-toggleButton>  <p-toggleButton  [(ngModel)]="checked2"  onIcon="pi pi-caret-left"  offIcon="pi pi-caret-right"  onLabel="ToggleButton Component"  offLabel="GeeksforGeeks"></p-toggleButton>
app.component.ts
import { Component } from "@angular/core";import { SelectItem, PrimeNGConfig } from "primeng/api";  @Component({  selector: "app-root",  templateUrl: "./app.component.html",})export class AppComponent {  constructor(private primengConfig: PrimeNGConfig) {}    ngOnInit() {    this.primengConfig.ripple = true;  }    checked1: boolean = false;    checked2: boolean = false;}
app.module.ts
import { NgModule } from "@angular/core";import { BrowserModule } from "@angular/platform-browser";import { FormsModule } from "@angular/forms";import { BrowserAnimationsModule }     from "@angular/platform-browser/animations";  import { AppComponent } from "./app.component";  import { ToggleButtonModule } from "primeng/togglebutton";  @NgModule({  imports: [    BrowserModule,    BrowserAnimationsModule,    ToggleButtonModule,    FormsModule,  ],  declarations: [AppComponent],  bootstrap: [AppComponent],})export class AppModule {}
Output:
Reference: https://primefaces.org/primeng/showcase/#/togglebutton
Angular-PrimeNG
AngularJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Angular PrimeNG Dropdown Component
Angular PrimeNG Calendar Component
Angular 10 (blur) Event
How to make a Bootstrap Modal Popup in Angular 9/8 ?
Angular PrimeNG Messages Component
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills