Ramanaa Gj Ramanaa Gj - 2 months ago 34
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


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>

In your controller:

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