Open In App

Angular PrimeNG Rating Component

Last Updated : 14 Feb, 2023
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 Rating component in Angular PrimeNG. Let’s learn about the properties, events & styling along with their syntaxes that will be used in the code. 

Rating component: It is used to represents the rating given by the user.

Properties:

  • stars: It indicates the number of stars to be shown. It is of number datatype, the default value is 5.
  • cancel: It shows a cancel icon that resets all the stars values. It is of the boolean data type, the default value is true.
  • disabled: When it is set to be true, the rating should be disabled. It is of the boolean data type, the default value is false.
  • readonly: We cannot change the value of the component. It is of the boolean data type, the default value is false.
  • iconOnClass: It is used to set the class of the ‘on’ icon. It is of string data type, the default value is pi pi-star.
  • iconOffClass: It is used to set the class of the ‘off’ icon. It is of string data type, the default value is pi pi-star.
  • 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.
  • iconOnStyle: It is used to set the inline style of the ‘on’ icon. It is of object data type, the default value is null.
  • iconOffStyle: It is used to set the inline style of the ‘off’ icon. It is of object data type, the default value is null.
  • iconCancelStyle: It is used to set the inline style of the ‘cancel’ icon. It is of object data type, the default value is null.

Events:

  • onRate: It is a callback that is fired on rate change.
  • onCancel: It is a callback that is fired when the value is removed.

 

Styling:

  • p-rating: It is a container element.
  • p-rating-star: It is a star element.
  • p-rating-star-on: It shows the selected star element.
  • p-rating-cancel: It shows the cancel icon.

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: This is the basic example that shows how to use the Rating component. 

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Rating component</h5>
<p-rating [cancel]="false"></p-rating>


app.component.ts




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


 

app.module.ts




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: In this example, we will know how to use the cancel property in the Rating component.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Rating component</h5
<p-rating cancel='true'></p-rating>


app.component.ts




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


app.module.ts




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 3: In this example, we will know how to use the readOnly property in the Rating component.

app.component.html




<h2>GeeksforGeeks</h2>
<h5>PrimeNG Rating component</h5>
<p-rating readonly="true" cancel="true"></p-rating>


app.component.ts




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


app.module.ts




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://primeng.org/rating



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

Similar Reads