Open In App

Angular PrimeNG Form ToggleButton Properties

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:

 



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

ng serve --open

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




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




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 { }




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.




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




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 { }




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


Article Tags :