angularJS rating stars
Naive angularJS implementation of star ratings widget
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div ng-app="StarRatings">
<div ng-controller="myController as ctrl">
<div ng-repeat="n in ctrl.getStarArray()" ng-class="ctrl.getClass(n)" ng-mouseover="ctrl.setClass($event,n)"></div>
<p>
You have chosen {{ctrl.selStars}} stars
</p>
</div>
</div>
CSS
.glyphicon {
color: gold;
cursor: pointer;
font-size: 1.25em;
}
JavaScript
(function() {
var app = angular.module('StarRatings', []);
app.controller('myController', function() {
this.selStars = 0; // initial stars count
this.maxStars = 5;
this.getStarArray = function() {
var result = [];
for (var i = 1; i <= this.maxStars; i++)
result.push(i);
return result;
};
this.getClass = function(value) {
return 'glyphicon glyphicon-star' + (this.selStars >= value ? '' : '-empty');
};
this.setClass = function(sender, value) {
this.selStars = value;
sender.currentTarget.setAttribute('class', this.getClass(value));
};
});
})();