Skip to content Skip to sidebar Skip to footer

Recursive Calling In Angularjs

I am new to Angulrajs and I want to list a JSON object in html page. I think I have to go into the object recursively. Is there any thing that can be done in Angularjs. I have nest

Solution 1:

You're going to use ng-repeat to loop over your object. Like this.

<ul>
  <li ng-repeat="stuff in things"> stuff </li>
</ul>

*after your edit. You think you have to use recursion because you have nested objects. That's not entirely true. You could use multiple ng-repeats. Something like this.

 <div>
  <div ng-repeat="stuff in things">
    <span> stuff </span>
    <div ng-repeat="morestuff in stuff">
      moreStuff
    </div>
 </div>

Solution 2:

If you are still looking for true recursion, here's what you could do (for example, recursively nested comments)

<div class="panel panel-default" 
ng-repeat="comment in comments" 
ng-include="'comment_recursion.html'">
</div>

<!-- == Recursion script  == -->
<script type="text/ng-template" id="comment_recursion.html">
    <div ng-controller="CommentCtrl">   
        <div class="panel-heading">
            {{comment.author}}
            <%= render "layouts/controverse_btns" %>
        </div>
        <div class="panel-body">
            {{comment.text}}
        </div>
        <div class="panel-footer">
            <span>Created :{{comment.created_at}}, </span>
            <span>Last Edited : {{comment.updated_at}}</span>
        </div>

    </div>
    <!-- Comment replies (other comments), might want to indent to the right --> 
    <div class="reply" ng-if="comment.replies">
        <div class="panel panel-default" 
            ng-repeat="comment in comment.replies" 
            ng-include="'comment_recursion.html'">
        </div>
    </div>
</script>

Post a Comment for "Recursive Calling In Angularjs"