سؤال

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?

هل كانت مفيدة؟

المحلول 2

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'}
  ];
});

نصائح أخرى

you need to link template directive with the scope value which is to supplied

i have created a small demo you can use $scope.rating option in a controller to change rating here

WORKING DEMO

template: '<ul class="rating">' +
              '<li ng-repeat="star in stars" ng-class="star">' +
                '\u2605' +
              '</li>' +
            '</ul>',
  scope: {
    ratingValue: '=',
    max: '='
  },
  link: function (scope, elem, attrs) {
    scope.stars = [];
    for (var i = 0; i < scope.max; i++) {
      scope.stars.push({filled: i < scope.ratingValue});
    }
  }

There is a wonderful tutorial here for more explaination

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top