Ramanaa Gj Ramanaa Gj - 1 month ago 20
AngularJS Question

ng-repeat a div or span according to the json value using angularjs

Want to show a div n times according to the value which was get from json

My json value

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


if a product has 3 ratings means the div have to show three times.like a star ratings.how to do in angular.js

My Plunker Demo

Answer

You can create an array, based on the rating. Then repeat over that array:

<div ng-repeat="array in pro">
    {{array.product}} , <span ng-repeat="n in createArray(array.rating) track by $index">X</span>
</div>

In your controller:

$scope.createArray = function(n){    
    return new Array(n);
}

https://plnkr.co/edit/Hjq97C4x1B1jauAQctbE?p=info