angularjs星级评分 star rating directive

directive

var app = angular.module("app", []);
app.controller('ctrl',function($scope){
  $scope.max = 5;
  $scope.ratingVal = 2;
  $scope.readonly = false;
  $scope.onHover = function(val){
    $scope.hoverVal = val;
  };
  $scope.onLeave = function(){
    $scope.hoverVal = null;
  }
  $scope.onChange = function(val){
    $scope.ratingVal = val;
  }
});
app.directive('star', function () {
  return {
    template: '<ul class="rating" ng-mouseleave="leave()">' +
        '<li ng-repeat="star in stars" ng-class="star" ng-click="click($index + 1)" ng-mouseover="over($index + 1)">' +
        '\u2605' +
        '</li>' +
        '</ul>',
    scope: {
      ratingValue: '=',
      max: '=',
      readonly: '@',
      onHover: '=',
      onLeave: '='
    },
    controller: function($scope){
      $scope.ratingValue = $scope.ratingValue || 0;
      $scope.max = $scope.max || 5;
      $scope.click = function(val){
        if ($scope.readonly && $scope.readonly === 'true') {
          return;
        }
        $scope.ratingValue = val;
      };
      $scope.over = function(val){
        $scope.onHover(val);
      };
      $scope.leave = function(){
        $scope.onLeave();
      }
    },
    link: function (scope, elem, attrs) {
      elem.css("text-align", "center");
      var updateStars = function () {
        scope.stars = [];
        for (var i = 0; i < scope.max; i++) {
          scope.stars.push({
            filled: i < scope.ratingValue
          });
        }
      };
      updateStars();
      scope.$watch('ratingValue', function (oldVal, newVal) {
        if (newVal) {
          updateStars();
        }
      });
      scope.$watch('max', function (oldVal, newVal) {
        if (newVal) {
          updateStars();
        }
      });
    }
  };
});

css:

.rating {
  color: #a9a9a9;
  margin: 0;
  padding: 0;
}
ul.rating {
  display: inline-block;
}
.rating li {
  list-style-type: none;
  display: inline-block;
  padding: 1px;
  text-align: center;
  font-weight: bold;
  cursor: pointer;
}
.rating .filled {
  color: #ffee33;
}

html:

<div ng-app="app">
<div class="well" ng-controller="ctrl">
  <div star rating-value="ratingVal" max="max" on-hover="onHover" on-leave="onLeave" readonly="{{readonly}}"></div>
  <div>
    Max: <input type="number" name="input" ng-model="max" min="0" max="99" required>
    <p>current value: {{ratingVal}}</p>
    <p>hover on : {{hoverVal?hoverVal:"none"}}</p>
    <p>readonly : <input type="checkbox"
       ng-model="readonly"></p>
  </div>
</div>
</div>

github page

http://shengoo.github.io/angularjs-practice/directive/star/star.html

jsfiddle:

code pen:

See the Pen angularjs star rating directive by Qing Sheng (@shengoo) on CodePen.

angularjs星级评分 star rating directive》有1个想法

  1. 因为要实现星星评分功能,找到了博主这篇文章,在调试过程中,发现了一个比较低级的错误。在使用$watch观察变量的值的时候,$watch的参数2(一个函数)的参数顺序应当是先newValue,后oldValue,而博主写反了。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注