Created
November 13, 2014 16:39
-
-
Save jeremygithub/fba5004896537a572ce6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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