Open In App

Angular PrimeNG Form RadioButton Basic Component

Last Updated : 01 Dec, 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 Form RadioButton Basic Component in Angular PrimeNG.

The RadioButton Component allows the user to select one option at a time from a set. This is an extension for the radio button element that has various theming.

Syntax:

<p-radioButton value="..." 
                inputId="..." disabled
               [(ngModel)]="...">
</p-radioButton>
<label for="...">....</label>

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 basic example illustrates the use of the Angular PrimeNG Form RadioButton Basic Component.

  • app.component.html:

HTML




<h1 style="color: green">
   GeeksforGeeks
</h1>
<h5>
   Angular PrimeNG Form Radiobutton Basic Component
</h5>
  
<p-radioButton value="val1" 
               inputId="gfg1" 
               [(ngModel)]="gfg">
</p-radioButton>
<label for="gfg1">Angular JS</label>
   
<p-radioButton value="val2" 
               inputId="gfg2" 
               [(ngModel)]="gfg">
</p-radioButton>
<label for="gfg2">React JS</label>
   
<p-radioButton value="val3" 
               inputId="gfg3" 
               [(ngModel)]="gfg">
</p-radioButton>
<label for="gfg3">Ember JS</label>
   
<p-radioButton value="val4" 
               inputId="gfg4" 
               [(ngModel)]="gfg">
</p-radioButton>
<label for="gfg4">Node JS</label>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent { }


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


Output:

 

Example 2: This is another example that illustrates the use of the Angular PrimeNG Form RadioButton Basic Component using the disabled attribute.

  • app.component.html:

HTML




<h1 style="color: green">
   GeeksforGeeks
</h1>
<h5>
   Angular PrimeNG Form Radiobutton Basic Component
</h5>
  
<p-radioButton value="val1" 
               inputId="gfg1" 
               [(ngModel)]="gfg">
</p-radioButton>
<label for="gfg1">System Design</label>
<br /><br />
<p-radioButton value="val2" 
               inputId="gfg2" 
               [(ngModel)]="gfg">
</p-radioButton>
<label for="gfg2">DSA Self-Paced</label>
<br /><br />
<p-radioButton value="val3" 
               inputId="gfg3" disabled 
               [(ngModel)]="gfg">
</p-radioButton>
<label for="gfg3">C++ STL</label>
<br /><br />
<p-radioButton value="val4" 
               inputId="gfg4" 
               [(ngModel)]="gfg">
</p-radioButton>
<label for="gfg4">Web Development</label>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent { }


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


Output:

 

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



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

Similar Reads