jquery raty插件通过angularjs设置速率值

时间:2013-10-16 08:20:03

标签: javascript angularjs raty

我正在尝试使用jquery raty插件进行Starsjs的星级评分。我为它创建了一个自定义指令:

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}}"设置得分值,但这不起作用。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

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

链接起来

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

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

有一个很棒的教程here可以进行更多解释

答案 1 :(得分: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'}
  ];
});