ng-rating-bar
Version:
Star rating component for angular
211 lines (180 loc) • 4.14 kB
Markdown
[](https://stackblitz.com/edit/ng-rating-bar?file=src%2Fapp%2Fapp.component.html "Demo")
```
npm i ng-rating-bar
```
```
import { NgRatingBarModule } from 'ng-rating-bar';
@NgModule({
declarations: [...],
imports: [
BrowserModule,
NgRatingBarModule
]
})
```
```
<ng-rating-bar
[(value)]="value"
[]="10"
></ng-rating-bar>
Value is {{ value }}
```
| Name | Type | Required | Default |
| ------------- | ------------- | ------------- | ------------- |
| value | number | Required | 0 |
| ratingCount | number | Required | 5 |
| disabled | boolean | Optional | false |
| resetAble | boolean | Optional | false |
| colorActive | string | Optional |
| colorDefault | string | Optional |
| styles | Object | Optional | { fontSize: '28px', backgroundColor: '', margin: '5px', padding: '0px' } |
| Event | Parameter | Description |
| ------ | --------- | ----------- |
| valueChange | (value: number) | Callback to invoke when value was changed |
| hoverChange | (value: number) | Triggered when on hover change |
**Note** For angular version below 14 use version `1.0.2`
```
npm i ng-rating-bar@1.0.2
```
```
<ng-rating-bar
[]="value"
(valueChange)="onValueChange($event)"
[]="10"
></ng-rating-bar>
```
In component
```
onValueChange($event: number) {
this.value = $event
}
```
```
<ng-rating-bar
[]="5"
[]="7"
[]="true"
></ng-rating-bar>
```
In html view
```
<form [formGroup]="myForm">
<ng-rating-bar
[]="myForm.get('rating')"
[]="ratingCount"
></ng-rating-bar>
<p *ngIf="myForm.get('rating').touched && myForm.get('rating').hasError('required')">
Field is required
</p>
<p>
<button (click)="submitForm()">Submit</button>
</p>
</form>
```
In Component
```
ngOnInit() {
this.myForm = this.fb.group({
rating: [null, Validators.required]
});
}
submitForm() {
this.myForm.get('rating').markAllAsTouched();
console.log(this.myForm.value);
}
```
Same example for angular ^14
```
<form [formGroup]="myForm">
<ng-rating-bar
[]="control"
[]="ratingCount"
></ng-rating-bar>
<p *ngIf="myForm.get('rating').touched && myForm.get('rating').hasError('required')">
Field is required
</p>
<p>
<button (click)="submitForm()">Submit</button>
</p>
</form>
```
In component define getter for current formControl
```
get control() {
return this.myForm.get('rating') as FormControl;
}
```
```
Value is:
<b
[]="hoverValue === 7"
[]="hoverValue > 4 && hoverValue < 7"
[]="hoverValue > 2 && hoverValue <= 4 "
[]="hoverValue <= 2"
>
{{ hoverValue }}
</b>
<p>
<ng-rating-bar
[(value)]="value2"
(hoverChange)="hoverValue = $event"
[]="7"
></ng-rating-bar>
</p>
```
```
<ng-rating-bar
[(value)]="value"
[]="ratingCount"
colorActive="red"
colorDefault="gray"
></ng-rating-bar>
```
```
<ng-rating-bar
value="5"
[]="10"
[]="{backgroundColor: '#0965ee', margin: '10px', fontSize: '32px', padding: '2px'}"
></ng-rating-bar>
```
Example of html symbols [https://www.w3schools.com/charsets/ref_utf_symbols.asp](https://www.w3schools.com/charsets/ref_utf_symbols.asp)
```
<ng-rating-bar
value="5"
[]="10"
[]="'☀'"
[]="true"
></ng-rating-bar>
```
If you have installed font awesome into your project you can pass icon as `@Input()`
In component
```
export class AppComponent implements OnInit {
faIcon = '<i class="fa fa-car"></i>';
constructor() {}
....
}
```
In html
```
<ng-rating-bar
value="5"
[]="10"
[]="faIcon"
></ng-rating-bar>
```