Suppose I have a json like this:
{
"data": {
"data": {
"data": {
"set": "1"
}
}
}
}
I want to use ng-repeat to loop through this json, however I want to loop through it dynamically, I will not know how many data objects I will have, sometimes it could be 3, and sometimes it could be 5,6,7, etc.
How can I loop through this with ng-repeat without having to write ng-repeat as many times as there are data objects.
In javascript I would just write something like this:
function loop(data) {
if (data.data) {
loop(data.data)
}
}
Use the same function you would use and call it from the ng-repeat
<ul>
<li ng-repeat="v in array">
{{getValue(v)}}
</li>
</ul>
and the function in the controller like so:
$scope.getValue = function(item){
if(item.data){
return $scope.getValue(item.data);
}else{
return item.set;
}
};
Here is a link to a working example http://codepen.io/mkl/pen/dXOOVo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With