Open In App

Angular PrimeNG Form SelectButton Value Binding 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 Form SelectButton Value Binding Component in Angular PrimeNG.

The SelectButton Component is used to make a group of buttons from which the user can select a value. The optionValue property can be utilized to customize the property to pass as the value, as the option itself, by default, is bound to the model.

Angular PrimeNG Form SelectButton Value Binding Component Properties:

  • options: It is an array representing select items to display as the available options. It is of array data type, the default value is null.
  • optionLabel: It is used to give the label of a choice. It is of string data type, the default value is the label.
  • optionValue: It is used to give the value of an option, and defaults to the option itself when not defined. It is of string data type, the default value is value.
  • multiple: It is used to set the when specified, allowing the selection of multiple values. It is of the boolean data type, the default value is false.

 

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: This example describes the basic usage of the Angular PrimeNG Form SelectButton Value Binding Component.

  • app.component.html

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>
    Angular PrimeNG Form SelectButton
    Value Binding Component
</h5>
  
<p-selectButton [options]="gfg" 
                optionLabel="label" 
                optionValue="value">
</p-selectButton>


  • app.component.ts

Javascript




import { Component } from '@angular/core';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    gfg: any[];
    constructor(private primeNGConfig: PrimeNGConfig) {
        this.gfg = [
            { label: 'Hi Geek1', value: 'one' },
            { label: 'Hi Geek2', value: 'two' },
            { label: 'Hi Geek3', value: 'three' },
        ];
    }
}


  • 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 { SelectButtonModule } 
    from 'primeng/selectbutton';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        SelectButtonModule,
        ButtonModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }


Output:

 

Example 2: Below is another code example that illustrates the use of Angular PrimeNG Form SelectButton Value Binding Component.

  • app.component.html

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>
    Angular PrimeNG Form SelectButton
    Value Binding Component
</h5>
  
<p-selectButton [options]="gfg" 
                optionLabel="label" 
                optionValue="iconValue" 
                [multiple]="true">
</p-selectButton>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import { PrimeNGConfig } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    gfg: any[];
    constructor(private primeNGConfig: PrimeNGConfig) {
        this.gfg = [
            { icon: 'pi pi-instagram', iconValue: 'instagram' },
            { icon: 'pi pi-whatsapp', iconValue: 'whatsapp' },
            { icon: 'pi pi-twitter', iconValue: 'twitter' },
        ];
    }
}


  • 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 { SelectButtonModule } 
    from 'primeng/selectbutton';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        SelectButtonModule,
        ButtonModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }


Output:

 

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



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