0

我正在尝试使用 jquery raty 插件对 angularjs 进行星级评分。我为它创建了一个自定义指令,如下所示:

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

我的html如下

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

score如果我将属性值预定义为,插件运行良好score="5",但我需要通过 angularjs 设置分值,score="{{place.rate}}"但这不起作用。

我怎么解决这个问题?

4

2 回答 2

0

您需要将模板指令与要提供的范围值链接

我创建了一个小演示,您可以在控制器中使用 $scope.rating 选项在此处更改评级

工作演示

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

有一个精彩的教程here以获得更多解释

于 2013-10-16T08:49:16.563 回答
0

UI Bootstrap具有很棒的功能,包括评分功能。

指示:

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

控制器:

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'}
  ];
});
于 2015-05-08T14:21:38.620 回答