Open In App

Angular PrimeNG Form Rating ReadOnly Component

Improve
Improve
Like Article
Like
Save
Share
Report

Angular PrimeNG is an open-source library that consists 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 Angular PrimeNG Form Rating ReadOnly Component.

The Form Rating Component is a selection input based on the star icon. It can be usually seen on product pages of e-commerce websites like Amazon, Flipkart, Alibaba, etc.

Syntax:

<p-rating 
    [ngModel]="..."
    stars="..."
    readonly="true"
    iconCancelClass="...">
</p-rating>

 

Angular PrimeNG Form Rating ReadOnly Component properties:

  • stars: It indicates the number of stars to be shown. It is of number datatype, the default value is 5.
  • iconCancelClass: It is used to set the class of the ‘cancel’ icon. It is of string data type, the default value is pi pi-ban.
  • readonly: We cannot change the value of the component. It is of the boolean data type, the default value is false.

Creating Angular Application and Installing the Module:

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: Finally, Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: The project Structure will look like this after following the above steps:

 

To run the above file run the below command:

ng serve --save

Example 1: Below is the code example that illustrates the use of the Angular PrimeNG Form Rating ReadOnly Component.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Form ReadOnly Component</h5>
  
<p-rating 
    [ngModel]="gfg"
    stars="5" 
    readonly="true" 
    iconCancelClass="pi pi-times">
</p-rating>


  • app.component.ts:

Javascript




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


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


Output:

 

Example 2: Below is another code example that illustrates the use of the Angular PrimeNG Form Rating ReadOnly Component without the cancel icon.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Form ReadOnly Component</h5>
  
<p-rating 
    [ngModel]="gfg" 
    stars="8" 
    readonly="true"
    [cancel]="false">
</p-rating>


  • app.component.ts:

Javascript




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


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


Output:

 

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



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