Open In App

Angular PrimeNG Form ToggleButton Basic Component

Improve
Improve
Like Article
Like
Save
Share
Report

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 Basic Component in Angular PrimeNG.

The ToggleButton Component is used to make a button that users can toggle by clicking on it. 

Angular PrimeNG Form ToggleButton Basic Component 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.

 

Creating Angular Application & module installation:

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 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

  • To run the above file run the below command:
ng serve --save

Example 1: Below is the example code that illustrates the use of Angular PrimeNG Form ToggleButton Basic Component.

  • app.component.html

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG ToggleButton Basic</h5>
  
<p-toggleButton onIcon="pi pi-book" 
                offIcon="pi pi-pencil">
</p-toggleButton>


  • app.component.ts

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
  
export class AppComponent { }


  • app.module.ts

Javascript




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: Below is another example code that illustrates the use of Angular PrimeNG Form ToggleButton Basic Component.

  • app.component.html

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG ToggleButton Basic</h5>
  
<p-toggleButton onLabel="GeeksforGeeks" 
                offLabel="GfG">
</p-toggleButton>


  • app.component.ts

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent { }


  • app.module.ts

Javascript




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/togglebutton



Last Updated : 03 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads