Open In App

Angular PrimeNG Form ToggleButton Properties

Last Updated : 01 Nov, 2022
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 see how to use the ToggleButton properties in Angular PrimeNG.

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

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

 

Creating Angular application & module installation:

Step 1: To create an angular app, you need to install the angular command line interface through the npm command.

npm install -g angular-cli

Step 2: Now we will create an angular project.

ng new project_name

Step 3: After creating your angular project, move into the folder to perform different operations.

cd project_name

Step 4: After creating the Angular application, Install the required module using the following command:

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

Project Structure: After running the commands mentioned in the above steps, it will look like the following:

Project Structure

  • Step to Run Application: Run the application using the following command from the root directory of the project:
ng serve --open

Example 1: We are creating a UI that shows Angular PrimeNG Toggle Button Properties. 

  • app.component.html

HTML




<div style="margin:100px; 
            text-align: center;">
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <h3>
          Angular PrimeNG Form 
        ToggleButton Properties
      </h3>
    <p-toggleButton [(ngModel)]="checked"
                    onIcon="pi pi-check" 
                    offIcon="pi pi-times" 
                    onLabel="Switched On"
                     iconPos="right" 
                    offLabel="Switched Off">
    </p-toggleButton>
</div>


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule } from "@angular/forms";
import { ToggleButtonModule } 
    from "primeng/togglebutton";
@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        ToggleButtonModule,
        FormsModule,
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }


  • app.component.ts

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    checked: boolean = true;
}


Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

 

Example 2: We are creating a UI that shows Angular PrimeNG Toggle Button Properties.

  • app.component.html

HTML




<div style="margin:100px; 
            text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>    
    <h3>
          Angular PrimeNG Form 
        ToggleButton Properties
      </h3>
    <br />
      
    <p-toggleButton [disabled]="true" 
                    offIcon="pi pi-times" 
                    onLabel="Switched On" 
                    offLabel="Disabled ToggleButton">
    </p-toggleButton>
    <br /><br />
    <p-toggleButton [(ngModel)]="checked" 
                    onIcon="pi pi-eye" 
                    offIcon="pi pi-eye-slash">
    </p-toggleButton>
</div>


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } 
    from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule } from "@angular/forms";
import { ToggleButtonModule }
    from "primeng/togglebutton";
@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        ToggleButtonModule,
        FormsModule,
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }


  • app.component.ts

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    checked: boolean = true;
}


Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

 

Reference: https://primefaces.org/primeng/togglebutton



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads