Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ng-repeat support recursion on itself?

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) } }

like image 308
mmmtoasted Avatar asked Jun 21 '16 22:06

mmmtoasted


1 Answers

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

like image 137
Michael Avatar answered Nov 14 '22 13:11

Michael