Here I am going to explain how to create rating feature using angular js directive concept and send the rating value to backend ( service ). I have explained same using Angular 2(+) concept as well, if you want to compare both implementation check this link as well.
Refer this link to know how it is implemented in Angular 2(+):- Rating star component using Angular 2(+).
Angular js 1 Directive
angular.module('samplemodule').directive('starRating',
function() {
return {
restrict : 'A',
template : '<ul class="rating">'
+ ' <li ng-repeat="star in stars"
ng-class="star" ng-click="toggle($index)">'
+ ' <i class="fa fa-star-o"></i>'
+ ' </li>'
+ '</ul>',
scope : {
ratingValue : '=',
max : '=',
onRatingSelected : '&'
},
link : function(scope, elem, attrs) {
var updateStars = function() {
scope.stars = [];
for ( var i = 0; i < scope.max; i++) {
scope.stars.push({
filled : i < scope.ratingValue
});
}
};
scope.toggle = function(index) {
scope.ratingValue = index + 1;
scope.onRatingSelected({
rating : index + 1
});
};
scope.$watch('ratingValue',
function(oldVal, newVal) {
if (newVal) {
updateStars();
}
}
);
}
};
}
);
Here i used Font awesome icon ( <i class="fa fa-star-o"></i> ).Font Awesome gives you scalable vector icons that can instantly be customized — size, color, drop shadow, and anything that can be done with the power of CSS.
Font awsome icons
You can improve the performance of your site by using font awesome images.
View
<div ng-init="rating = star.rating + 1"></div>
<div class="star-rating" star-rating rating-value="rating"
data-max="10" on-rating-selected="rateFunction(rating)"></div>
Only for displaying the rating you can use the class as star-rating.Here i am going to explain how to send this rating to backend (Service).
Controller
$scope.rateFunction = function( rating )
{
var _url = 'your service url';
var data = {
rating: rating
};
$http.post( _url, angular.toJson(data), {cache: false} )
.success( function( data )
{
success(data);
})
.error(function(data){
error(data);
});
};
CSS
Using this "filled" class we are changing the color of the rated stars .
.rating .filled {
color: #21568b;
}
Directives are markers on a DOM element that tell AngularJS's HTML compiler to attach a specified behavior to that DOM element or even transform the DOM element and its children. Better define all Global controllers in Directives. Ex: Warning message, alert etc.
'A' - Attribute (Use it as <div starrating>)
'E' - Element (Use it as <starrating>)
'C' - Class. (Use it as <div class="starrating">)
'M' - Comment (Use it as <!- directive: starrating -> )
We used watch concept in directive .Watch is a powerful feature of angular js. “$watch” concept in Angular is to observe the changes on the data of the scope from the controller. That is controller can add a listener on an attribute of its scope.
Related Posts
1. Communicate with controllers in angular js
4. Filtering concept in angular js
5. Save highchart as binary image
Thank you ...
ReplyDeleteIt works for me .
Hi ,
ReplyDeleteIs there any way to remove the the first star which is by default selected .I want none of the stars to be selected initially.
star.rating-1 instead of star.rating+1 in your html
DeleteHi Prashobh PS
DeleteWhat it takes to become the votes recorded, and may have an overall average with the votes of each stored users?
What should I put in: 'your service url' ?
Thanks
"your service url" is nothing but your webservice call .
DeleteYou will be getting count in $scope.rateFunction ,here you can do your logic to calculate average and all
nice
ReplyDeleteHi ,
ReplyDeleteIs there any way to show star from selected value which is get from angular service from e.g
{{feedback.feedback.rating}} (Getting from mongoDB Database)
yes .
Deletediv ng-init="rating = star.rating
Here you can assign the dynamic value(1,2,3 etc ).
Thanks
ReplyDeleteIt's works perfect.
Its awesome post! Thank you!
ReplyDeleteill give separate partial for that ..But i have error"+"+".
ReplyDeleteGreat directive!
ReplyDeleteHow can i use multipile rating in one page?
Thanks
its Nice, but i want smiles insted of stars.
ReplyDeletehow i get it?
"fa fa-star-o" here I used font awesome icon ,instead of use whatever the image you want .
DeleteAwesome !!
ReplyDeleteBut i need this in tooltip??
Tks!
ReplyDeletewhen i click on star the star getting selected but controller function not called
ReplyDeleteon-rating-selected="rateFunction(rating)"
Not called in my application why?
it is working for me ,if you can share a fiddle i can check
DeleteHi... I am new user of angularjs
ReplyDeleteyou'd use some attributes in HTML like star-rating, rating-value, on-rating-selected so I've to ask one question is that where you define or declare all those attributes, I'd didn't found any declaration or definition for such above mentioned attributes...
star-rating is the directive name (starRating) ,rating-value and on-rating-selected you can see inside directive (scope.ratingValue , scope.onRatingSelected).
DeleteHope it clears your doubt
Hi,
ReplyDeleteHow to fill if rating value is 4.333, have to fill exact value.Can anyone pls suggest.
Thanks,
Siva
How can i make any star rating static or read only for any current or max value?? Please reply soon .. Urgently needed....
ReplyDeleteyou can try with angular ng-disabled or use any css properties like pointer-event :none ect
Deleteremove `ng-click="toggle($index)" ` in directive template. that will automatically disable the user to click it
DeleteWould this work for Internet Explorer too ?
ReplyDeleteThe star image is not being displayed, only its {{value}}. Eg. {{5}}
it should work for ie9 and above ,I didn't test on ie8 and below.I used font awesome image ,you can try with any other
DeleteWhat should I do, if I want hover for the stars?? Can you help please...
ReplyDeletesimply add title on the html
Deleteli title="somthing" -> directive template html
Can you please add fiddle, I am new to angularjs.
DeleteFiddle is available already ,you can play with that.
DeleteI did it, but I want to hover on stars like in normal star rating hover.
DeleteSoooper really nic work :)
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi,
ReplyDeleteReally nice directive and I appreciate your work also.
I do need to implement it in mobile devices i.e stars would be filled on Touch.
Can u plz suggest me how to change directive so that it toggle() function can be called on touch instead of ng-click.
Thanks..
https://docs.angularjs.org/api/ngTouch
DeleteGood post. I really like your post. I like the way you describing this post. Its really informative for the followers.Thanks for sharing this awesome post.
ReplyDeleteApplication Development using Angularjs
Hi is there a way to present as half star such as a 3.5 which would related to 3 stars and a half???
ReplyDeleteCan someone guide me if there is a how-to?
Hi is there a way to display a half star such as 3 stars and a half (3.5) ??
ReplyDeleteSome times it doesn't updates or even doesn't shows rating if ratingValue is 0
ReplyDeleteJust update:
scope.$watch('ratingValue',
function(oldVal, newVal) {
if (newVal || newValue == 0) {
updateStars();
}
}
);
The oldVal and newVal parameters on your watch function are reversed. The newValue parameter should come first and the oldValue should come second. You can see that this causes issues if you start with a rating of 0 or null.
ReplyDeletethanks , will update it
DeleteCSS for make stars hoover
ReplyDelete.rating {
color: lightgrey;
margin: 0;
padding: 0;
font-size: 30px;
}
ul.rating {
display: inline-block;
}
.rating li {
list-style-type: none;
display: inline-block;
padding: 1px;
text-align: center;
font-weight: bold;
cursor: pointer;
}
.rating .filled {
color: #f44336;
font-size: 30px;
}
.star-rating:hover li {
/*color: #f39c12;*/
color: #f44336; /* amarillo (seleccionado) */
}
.star-rating:hover li:hover~li {
color: lightgrey; /* gris (normal) */
}
how do i record that star rating in a log file...
ReplyDelete"$scope.rateFunction" This method will give selected count.
Delete