Angular PrimeNG Form SelectButton Events
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 learn about Angular PrimeNG Form SelectButton Events
The SelectButton component is used to make a group of buttons from which the user can select a value.
SelectButton Events:
- onChange: it is a callback that is fired when the value changes.
- onOptionClick: it is a callback that is fired when a button is clicked.
Syntax:
<p-selectButton optionLabel="..." (event)=function() > </p-selectButton>
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
Example 1: In this article, we will learn Form SelectButton onChange Events
app.component.html
HTML
< div > < h1 style = "color:green" >w3wiki</ h1 > < h2 >Angular PrimeNG SelectButton Events</ h2 > < p-selectButton [options]="gfg" optionLabel = "label" (onChange)=onChange()> </ p-selectButton > </ div > |
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-linkedin' , value: 'pi pi-linkedin' }, { icon: 'pi pi-facebook' , value: 'pi pi-facebook' }, ]; } onChange() { alert( "Hi Geek!! I am called" ) } } |
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: In this article, we will learn Form SelectButton onOptionClick Events
app.component.html
HTML
< div > < h1 style = "color:green" >w3wiki</ h1 > < h2 >Angular PrimeNG SelectButton Events</ h2 > < p-selectButton [options]="gfg" optionLabel = "label" (onOptionClick)=onOptionClick()> </ p-selectButton > </ div > |
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-linkedin' , value: 'pi pi-linkedin' }, { icon: 'pi pi-facebook' , value: 'pi pi-facebook' }, ]; } onOptionClick() { alert( "Hi Geek!! I am called" ) } } |
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: http://primefaces.org/primeng/selectbutton
Contact Us