Open In App

Angular PrimeNG Button Properties of p-button

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. This article will show us how to style the Tooltip component in Angular PrimeNG.

A Button is generally used for carrying out some action when clicked. Buttons in the PrimeNG library come in different shapes ,sizes and many other properties. 

Properties of p-Button:

  • type: It is used to define the type of button. It is of string type and its default value is null.
  • label: It is used to add the text of the button. It is of string type and its default value is null.
  • icon: It is used to add the name of the icon. It is of string type and its default value is null.
  • iconPos: It is used to indicate the position of the icon. It is of string type and its default value is left.
  • style: It is used to set the inline style of the component. It is of string type and its default value is null.
  • styleClass: It is used to define the style class of the component. It is of string type and its default value is null.
  • badge: It is used to define the value of the badge. It is of string type and its default value is null.
  • badgeClass: It is used to define the badge style. It is of string type and its default value is null.
  • disabled: It is used to disable the button. It is of boolean type and its default value is false.
  • loading: It is used to set the loading state of the button to true or false. It is of boolean type and its default value is false.
  • loadingIcon: It is used to change the default loading button icon. It is of string type and its default value is pi pi-spinner pi-spin.
  • onClick: It is used to execute callback on click of button. It is of event type and its default value is null.
  • onFocus: It is used to execute callback on focus of button. It is of event type and its default value is null.
  • onBlur: It is used to execute callback when button loses focus. It is of event type and its default value is null.
  • ariaLabel: It is used to define a string that autocomplete attribute the current element. It is of string type and its default value is null.

Approach: Let us create an Angular project and install PrimeNG UI module. Then we will create a UI that will showcase Angular PrimeNG Properties of p-button.

Creating Angular Project and Installing Module:

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

npm install -g angular-cli

Step 2: Now we will create a angular project.

ng new project_name

Step 3: After creating your react 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, if you open the project in an editor, you can see a similar project structure as shown below. The new component user makes or the code changes, we will be performing will be done in the source folder.

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 above mentioned Properties of p-Button.

  • app.component.html

HTML




<div style="margin:100px; text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
      
    <h3>
        Angular PrimeNG Button 
        Properties of p-Button
    </h3><br />
      
    <p>Disabled Button:</p>
    <p-button label="Disabled" 
        icon="pi pi-ban" [disabled]="true">
    </p-button>
      
    <p>Button with Loading state: </p>
    <p-button label="Loading.." 
        [loading]="true">
    </p-button>
    <br /><br />
      
    <p-button label="Loading.." 
        [loading]="true" iconPos="right">
    </p-button>
    <br /><br />
      
    <p> Button with Loading state & Custom icon:</p>
    <p-button loadingIcon="pi pi-spin pi-sun" 
        [loading]="true">
    </p-button>
</div>


  • app.module.ts

Javascript




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


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 above mentioned Properties of p-Button.

  • app.component.html

HTML




<div style="margin:100px; text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
      
    <h3>
        Angular PrimeNG Button 
        Properties of p-Button
    </h3><br />
      
    <p>Button with Badge:</p>
    <p-button label="Users" badge="8" 
        styleClass="p-button-success" 
        badge="100" badgeClass="p-badge-warning">
    </p-button>
    <br /><br />
      
    <p>Button onClick Event:</p>
    <p-button label="Click here" 
        icon="pi pi-arrow-circle-right" iconPos="right" 
        (onClick)="handleClick($event)">
    </p-button>
</div>


  • app.module.ts

Javascript




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ButtonModule } from "primeng/button";
@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        ButtonModule,
    ],
    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 {
    handleClick(e: any) {
        console.log("Button clicked")
    }
}


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

 

Reference: http://primefaces.org/primeng/button



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