【问题标题】:Rendering a Star Rating System using angularjs使用 angularjs 渲染星级评分系统
【发布时间】:2014-07-02 00:30:22
【问题描述】:

我的应用在这个Fiddle 我需要从 http 服务动态呈现星级评分系统,其中当前的星级和最大星级可能因每种情况而异。

$scope.current 和创建数组是个好主意吗? $scope.max - $scope.current 并传递它们并在它们上运行 ng-repeat,或者有比这更优化的解决方案。 Iteration ng-repeat only X times in AngularJs

【问题讨论】:

  • 您应该在问题中包含小提琴中的所有代码。
  • @AshokKumarSahoo 看看我的回答

标签: javascript angularjs


【解决方案1】:

星级评分可以静态(只读)或动态完成

如果您只想将评分显示为星级,请尝试以下方法

静态星级

Working Example

html

<body ng-app="starApp">
    <div ng-controller="StarCtrl"> <span ng-repeat="rating in ratings">{{rating.current}} out of
            {{rating.max}}
        <div star-rating rating-value="rating.current" max="rating.max" ></div>
        </span>

    </div>
</body>

脚本

var starApp = angular.module('starApp', []);

starApp.controller('StarCtrl', ['$scope', function ($scope) {
    $scope.ratings = [{
        current: 5,
        max: 10
    }, {
        current: 3,
        max: 5
    }];
}]);

starApp.directive('starRating', function () {
    return {
        restrict: 'A',
        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
                });
            }
        }
    }
});

如果你想动态地进行星级评分,试试这个

动态星级评分

Working Demo

HTML

<body ng-app="starApp">
    <div ng-controller="StarCtrl"> <span ng-repeat="rating in ratings">{{rating.current}} out of
            {{rating.max}}
        <div star-rating rating-value="rating.current" max="rating.max" on-rating-selected="getSelectedRating(rating)"></div>
        </span>
    </div>
</body>

脚本

var starApp = angular.module('starApp', []);

starApp.controller('StarCtrl', ['$scope', function ($scope) {
    $scope.rating = 0;
    $scope.ratings = [{
        current: 5,
        max: 10
    }, {
        current: 3,
        max: 5
    }];

    $scope.getSelectedRating = function (rating) {
        console.log(rating);
    }
}]);

starApp.directive('starRating', function () {
    return {
        restrict: 'A',
        template: '<ul class="rating">' +
            '<li ng-repeat="star in stars" ng-class="star" ng-click="toggle($index)">' +
            '\u2605' +
            '</li>' +
            '</ul>',
        scope: {
            ratingValue: '=',
            max: '=',
            onRatingSelected: '&'
        },
        link: function (scope, elem, attrs) {

            var updateStars = function () {
                scope.stars = [];
                for (var i = 0; i < scope.max; i++) {
                    scope.stars.push({
                        filled: i < scope.ratingValue
                    });
                }
            };

            scope.toggle = function (index) {
                scope.ratingValue = index + 1;
                scope.onRatingSelected({
                    rating: index + 1
                });
            };

            scope.$watch('ratingValue', function (oldVal, newVal) {
                if (newVal) {
                    updateStars();
                }
            });
        }
    }
});

有一个精彩的教程 here 以获得更多关于 Angular Star Rating

的解释

【讨论】:

  • Visual Studio 建议元素“div”不能嵌套在元素“span”内
【解决方案2】:

let app = angular.module ('myapp',[])
-
##star Rating Styles 
----------------------*/

.stars {
  padding-top: 10px;
  width: 100%;
  display: inline-block;
}
span.glyphicon {
  padding: 5px;
}
.glyphicon-star-empty {
  color: #9d9d9d;
}
.glyphicon-star-empty,
.glyphicon-star {
  font-size: 18px;
}
.glyphicon-star {
  color: #FD4;
  transition: all .25s;
}
.glyphicon-star:hover {
  transform: rotate(-15deg) scale(1.3);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div ng-app= "myapp"  >
<div class="stars">
  <div id="stars" class="star">
    <span ng-repeat="x in [0,1,2,3,4,5]" ng-if="($index < 4)" class="glyphicon glyphicon-star">  </span>
    <span ng-repeat="x in [0,1,2,3,4,5]" ng-if="($index >= 4 && $index < 5) " class="glyphicon glyphicon-star-empty"></span>
  </div>
</div>
  </div>

【讨论】:

  • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
  • 只能使用 Angular html 指令来实现
【解决方案3】:

我的简约方法:

观点

<head>
  <!-- alternatively you may use another CSS library (like FontAwesome) to represent the star glyphs -->
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></link>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0-rc.2/angular.min.js"></script>
  <!-- insert the JavaScript controller and the CSS enhancements here -->
</head>

<body>
  <div ng-app="StarRatings">
    <div ng-controller="myController as ctrl">
      <div ng-repeat="n in ctrl.getStarArray()" ng-class="ctrl.getClass(n)" ng-mouseover="ctrl.setClass($event,n)">  </div>
      <p>
        You have chosen {{ctrl.selStars}} stars
      </p>
    </div>
  </div>
</body>

注意:如果您想使用onClick 事件而不是onMouseOver 事件,请在上面的HTML 中将ng-mouseover 替换为ng-click

控制器

<script>
(function() {
  var app = angular.module('StarRatings', []);

  app.controller('myController', function() {
    this.selStars = 0; // initial stars count
    this.maxStars = 5; // maximum number of stars

    // a helper function used within view
    this.getStarArray = function() {
      var result = [];
      for (var i = 1; i <= this.maxStars; i++)
        result.push(i);
      return result;
    };

    // the class used to paint a star (filled/empty) by its position
    this.getClass = function(index) {
      return 'glyphicon glyphicon-star' + (this.selStars >= index ? '' : '-empty');
    };

    // set the DOM element class (filled/empty star)
    this.setClass = function(sender, index) {
      this.selStars = index;
      sender.currentTarget.setAttribute('class', this.getClass(index));
    };

  });
})();
</script>

可选的一些 CSS 增强功能

<style>
.glyphicon {
  color: gold;
  cursor: pointer;
  font-size: 1.25em;
}
</style>

不相信?试试这个 JSFiddle:https://jsfiddle.net/h4zo730f/2/

【讨论】:

    【解决方案4】:

    hmc.starRating.js

    angular.module('starRatings',[]).directive('starRating', function () {
            return {
                restrict: 'A',
                template: '<ul class="rating">' +
                    '<li ng-repeat="star in stars" ng-class="star">' +
                    '\u2605' +
                    '</li>' +
                    '</ul>',
                scope: {
                    ratingValue: '=',
                    max: '='
                },
                link: function (scope, elem, attrs) {
                    console.log(scope.ratingValue);
                    function buildStars(){
                      scope.stars = [];
                      for (var i = 0; i < scope.max; i++) {
                          scope.stars.push({
                              filled: i < scope.ratingValue
                          });
                      }
                    }
                    buildStars();
                    scope.$watch('ratingValue',function(oldVal, newVal){
                      if(oldVal !== newVal){
                        buildStars();
                      }
                    })
                }
            }
        });
    
    
    
    <script src="hmc.starRating.js"></script>
    
    
    **app.js**
    var app = angular.module('app', ['ui.bootstrap', 'starRatings']);
    
    
    **indix.html**
    <div star-rating rating-value="7" max="8" ></div>
    
    
        .rating {
            color: #a9a9a9;
            margin: 0 !important;
            padding: 0 !important;
        }
        ul.rating {
            display: inline-block !important;
        }
        .rating li {
            list-style-type: none !important;
            display: inline-block !important;
            padding: 1px !important;
            text-align: center !important;
            font-weight: bold !important;
            cursor: pointer !important;
            width: 13px !important;
            color: #ccc !important;
            font-size: 16px !important;
        }
        .rating .filled {
          color: #ff6131 !important;
          width: 13px !important;
        }
    

    【讨论】:

      【解决方案5】:

       //Controller
              var starApp = angular.module('starApp', []);
              starApp.controller('StarCtrl', ['$scope', function ($scope) {
                  $scope.maxRating = 10;
                  $scope.ratedBy = 0;
                  $scope.rateBy = function (star) {
                      $scope.ratedBy = star;
                  }
              }]);
       .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: blue;
              }
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
      <body ng-app="starApp">    
          <div ng-controller="StarCtrl">
              <ul class="rating">
                  <li ng-repeat="n in [].constructor(maxRating) track by $index">
                      <span ng-click="rateBy($index+1)" ng-show="ratedBy > $index" class="filled">&#9733;</span>
                      <span ng-click="rateBy($index+1)" ng-show="ratedBy <= $index">&#9733;</span>
                  </li>
              </ul>
          </div>
          </body>

      【讨论】:

        【解决方案6】:

        您甚至可以尝试 angular-ui。这是link

        只需要添加这个标签。

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

        【讨论】:

          【解决方案7】:

          你可以像这样持有一个对象数组:

          var starApp = angular.module('starApp',[]);
          
          starApp.controller ('StarCtrl', ['$scope', function ($scope) {
              $scope.ratings = [];
              var rating = {
                   current : 5, 
                   max : 10
              }
              $scope.ratings.push(rating);  // instead you would push what your http service returns into thearray. 
          
          }]);
          

          那么在您看来,您可以像这样使用 ng-repeat:

          <body ng-app="starApp">
              <div ng-controller="StarCtrl">
                  <span ng-repeat="rating in ratings">{{rating.current}} out of {{rating.max}}</span>
              </div>
          </body>
          

          【讨论】:

          • 其实我想渲染星星而不是“当前超出最大值”
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-06
          相关资源
          最近更新 更多