javascript – 使用angularjs使用ng-repeat和json数据进行CSS星级评分

前端之家收集整理的这篇文章主要介绍了javascript – 使用angularjs使用ng-repeat和json数据进行CSS星级评分前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
想要根据从 JSON收到的值显示div.

我的目标:

$scope.pro = [
    {
        product: "chicken",rating: 3
    },{
        product: "fish",{
        product: "pizza",rating: 4
    }
];

如果一个产品有3个评级意味着div必须显示三次,如星级.
如何在angular.js中做到这一点?

My Plunker Demo

解决方法

你能试试吗

JS

$scope.pro = [{product: "chicken",rating: 3},{product: "fish",{product: "pizza",rating: 4}];

var ratingTotal = 5; 

$scope.getRepeater = function() {
   return new Array(ratingTotal);
};

HTML

<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <script data-require="angular.js@1.3.15" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="myController">

    <div ng-repeat="array in pro">{{array.product}} <span class="star-icon" ng-repeat="r in getRepeater() track by $index" ng-class="{'full': ($index + 1) <= array.rating,'half': ($index + .5) == array.rating}" ></span></div>

  </body>

</html>

注意:所选星标的类名称称为“完整”,可随意更改.

原文链接:https://www.f2er.com/js/155402.html

猜你在找的JavaScript相关文章