Skip to content

Instantly share code, notes, and snippets.

@jeremygithub
Created November 13, 2014 16:39
Show Gist options
  • Save jeremygithub/fba5004896537a572ce6 to your computer and use it in GitHub Desktop.
Save jeremygithub/fba5004896537a572ce6 to your computer and use it in GitHub Desktop.
angular.module('myApp')
.directive('starRating', function () {
return {
restrict: 'A',
template: '<ul class="rating"><li ng-repeat="star in stars" ng-class="star" class="icon ion-ios7-star" ng-click="toggle($index)"></li></ul>',
scope: {
ratingValue: '=',
max: '=',
readonly: '@',
onRatingSelected: '&'
},
link: function (scope) {
var updateStars = function () {
scope.stars = [];
for (var i = 0; i < scope.max; i++) {
scope.stars.push({
energized: i < scope.ratingValue,
'ion-ios7-star-half': scope.ratingValue % 1 > 0 && i === Math.floor(scope.ratingValue)
});
}
};
scope.toggle = function (index) {
if (angular.isUndefined(scope.readonly)) {
scope.ratingValue = index + 1;
scope.onRatingSelected({rating: index + 1});
}
};
scope.$watch('ratingValue', function () {
updateStars();
}
);
}
};
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment