Search code examples
javascriptangularjsraty

jquery raty plugin set the rate value via angularjs


I'm trying to use jquery raty plugin for star rating with angularjs. I created a custom directive for it as the following:

app.directive("ngStars", function() {
        return {
            restrict: 'A',
            link: function(scope, elem, attrs) {
                $(elem).raty({ 
                   'path': base_url+'images',
                    score:  attrs.score,
                    readOnly: true,
                    hints: ['سىء جدا', 'سىء', 'عادى', 'جديد', 'ممتاز'],
                });
            }
        }
    });

and my html as the following

<div ng-repeat="place in places">
    <div ng-stars class="star" score={{place.rate}}></div>
</div>

the plugin runs fine if i predefined the score attribute value as score="5", But what i need is to set the score value through angularjs score="{{place.rate}}" but this is not working.

How can i solve this problem?


Solution

  • UI Bootstrap have awesome features including rating feature.

    directive:

    <rating ng-model="rate" max="max" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null"></rating>
    

    controller:

    angular.module('ui.bootstrap.demo').controller('RatingDemoCtrl', function ($scope) {
      $scope.rate = 7;
      $scope.max = 10;
      $scope.isReadonly = false;
    
      $scope.hoveringOver = function(value) {
        $scope.overStar = value;
        $scope.percent = 100 * (value / $scope.max);
      };
    
      $scope.ratingStates = [
        {stateOn: 'glyphicon-ok-sign', stateOff: 'glyphicon-ok-circle'},
        {stateOn: 'glyphicon-star', stateOff: 'glyphicon-star-empty'},
        {stateOn: 'glyphicon-heart', stateOff: 'glyphicon-ban-circle'},
        {stateOn: 'glyphicon-heart'},
        {stateOff: 'glyphicon-off'}
      ];
    });