1

I'm trying to display all the reply from this json :

https://www.reddit.com/r/AskReddit/comments/k8w43d/people_with_the_last_name_pepper_who_have.json

So, depending of the post im loading and the reply, my number of children will differ. How can I loop throught it until I reach the last reply with children (body[2].data.children) ?

Like this :

<div class="replies-box" *ngFor="let reply of comments.body[1].data.children">
       <div class="reply-header">
            <p class="reply-author"><b>From</b> : {{ reply.data.author }}</p>
            <p class="reply-send"><b>Send</b> : {{ this.getDateReply(reply.data.created_utc) }}</p>
       </div>
       <div class="text-first-reply" [innerHTML]="this.getHtmlDecode(reply.data.body_html)">
         
       </div>
</div>

I have only the first level of reply, is there a way to simply loop through them all ? Thanks in advance.

2 Answers 2

1

I would use a recursion type of approach.

Develop a app-comment component and if comment has children, loop over the children and display the app-comment. That way it will loop over the comments until no more children

<div *ngIf='comment'>
  <span *ngIf='comment.kind; else showComment'>Kind: {{ comment.kind }}</span>
  <ng-template #showComment>
    <span>{{ comment }}</span>
  </ng-template>
  <div>
   <app-comment *ngFor='let child of comment.data?.children' [comment]='child'> </app-comment>
  </div>
</div>

See this sample illustration

Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect, exactly what I needed thanks a lot !
1

Simply use ngFor inside ngFor with help of ng-container (will not create extra dom elements)

<div class="replies-box">
  <ng-container *ngFor="let commentBody of comments.body">
    <ng-container *ngFor="let reply of commentBody.data.children">
      <div class="reply-header">
        <p class="reply-author">
          <b>From</b> : {{ reply.data.author }}
        </p>
        <p class="reply-send">
          <b>Send</b> : {{ this.getDateReply(reply.data.created_utc) }}
        </p>
      </div>
      <div class="text-first-reply" [innerHTML]="this.getHtmlDecode(reply.data.body_html)">

      </div>
    </ng-container>
  </ng-container>
</div>

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.