1

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 nested objects. So, I have to go into that.

1
  • just use recursion as you normally would in javascript. Commented Mar 14, 2014 at 4:23

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

6 Comments

ng-repeat does not offer recursion, only iteration.
Yes, which brings up the question of why recursion is needed. I would wager it's not in this case.
well, you can ask about it in a comment, rather than guessing (even though the OP has specifically stated he would "have to go into the object recursively").
Let's go ahead and not pick and choose what he said. I believe what he said was 'I THINK I have to go into the object recursively'. That big 'I think' was the reason I posted my answer. Odds are I'm wrong but if I am right ng-repeat is going to save him a ton of time.
I can also see where you're coming from. Thanks for the withdraw and have a good night or day :)
|
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.