0

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;
                }

            }
        }
};
});

1 Answer 1

1

You should use read-only-flag="true" to get the value into your directive. Please notice the dashes in the attribute.

Isolated scope camelCase are dashed in attributes.

Please have a look at the demo below or in this fiddle.

angular.module('demoApp', [])
.controller('mainController', function($scope) {
	$scope.readOnly = false;
	$scope.reviewItem = {
    	rating: 3
    };
})
.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){
            	console.log(scope.readOnlyFlag)
                if(!scope.readOnlyFlag){
                    scope.selectedIndex=index;
                    scope.rating=index+1;
                }

            }
        }
};
});
.filled  {
    color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet"/>
<div ng-app="demoApp" ng-controller="mainController">
    toggle read-only <input ng-model="readOnly" type="checkbox"/>
    <span starrating class="star-rating" read-only-flag="readOnly" rating="rating" ></span>
    <h2>
    always read-only
    </h2>
 <span starrating class="star-rating" read-only-flag="true" rating="reviewItem.rating" ></span>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.