Does ng-repeat support recursion on its own?

Suppose I have json: { "data": { "data": { "data": { "set": "1" } } } }

I want to use ng-repeat to loop through this json, however I want to dynamically scroll through it, I will not know how many data objects I will have, sometimes it can be 3, and sometimes it can be 5, 6,7, etc. d.

How can I loop 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) } }

+6
source share
1 answer

Use the same function that you would use and call it from ng-repeat

  <ul> <li ng-repeat="v in array"> {{getValue(v)}} </li> </ul> 

and functions in the controller:

 $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

+6
source

All Articles