想要根据从
JSON收到的值显示div.
我的目标:
$scope.pro = [ { product: "chicken",rating: 3 },{ product: "fish",{ product: "pizza",rating: 4 } ];
如果一个产品有3个评级意味着div必须显示三次,如星级.
如何在angular.js中做到这一点?
解决方法
你能试试吗
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>
注意:所选星标的类名称称为“完整”,可随意更改.