I am stuck with a problem where I have written a custom directive to rate with stars, where the user can rate from 1 to 5 by clicking on the stars, I was successful in achieving this.
<span starrating class="star-rating" rating="ratedValue" ></span>
But I am using the same directive to display the customer reviews, where I have a isolated scope variable called "rating", and this works well too.
<span starrating class="star-rating" readOnlyFlag="true" rating="reviewItem.rating" ></span>
But this time I dont want the user to click and change the rating. Hence I am declaring another variable in the isolated scope of the directive called "readOnlyFlag". But when I assign the value for readOnlyFlag in the directive as an attribute. I am unable get the value in the link function.
The directive code is below :
angular.module("pageDirectives",[]).directive("starrating", function(){
return {
restrict : 'A',
template :'<ul class="list-inline">'
+ ' <li ng-repeat="star in stars" ng-class="{filled: $index<=selectedIndex, filled:$index<rating}" ng-click="toggleIndex($index)">'
+ ' <i class="glyphicon glyphicon-star fa-2x"></i>'
+ ' </li>'
+ '</ul>',
scope : {
rating : '=',
readOnlyFlag : '=',
},
link: function (scope) {
scope.stars=[1,2,3,4,5];
scope.toggleIndex= function(index){
if(!scope.readonlyFlag){
scope.selectedIndex=index;
scope.rating=index+1;
}
}
}
};
});