0

I´ve been running around in iterated circles for several hours and have the sense the answer is staring me right in the face (and I have read a dozen similar questions) but I can't seem to see it.

I have this object:

{id: 1, firstName: "Rob", lastName: "Thompson", agent: 
"[email protected]", phone: null, …}
agent: "[email protected]"
comments: Array(2)
0: {id: 3, comment: "testing another comment"}
1: {id: 8, comment: "second comment"}
length: 2
__proto__: Array(0)
firstName: "Rob"
id: 1
lastName: "Thompson"
phone: null
__proto__: Object

that I am trying to display the comments on when I have selected that particular user.

I can access the first level fields easily and an individual comments via:

      {{gotClient.comments[0].comment}}

but in trying to display all comments associated with a user I am drawing a literal blank. This was the most recent attempt:

<div>
    <ol>
       <li *ngFor="let item of comments" > 
            <div>{{item.comments}}</div> 
               <ol>
                  <li *ngFor="let subItem of item['comments']"> 
                      <div>{{subItem.comment}}</div> 
                  </li>
                 </ol>
        </li>
       </ol>
   </div>

That´s producing literally a blank.

Please advise.

3
  • What is: let item of comments, Im guessing its the first comment level? Commented Jul 25, 2019 at 1:34
  • Yes, sir or ma'am - it is me attempting to iterative over the array: comments: Array(2) Commented Jul 25, 2019 at 1:35
  • There is also the chance I am way overthinking this. Commented Jul 25, 2019 at 1:36

1 Answer 1

1

Since comments is an array, the ngFor make you access each comment one by one(through item). Item is now: {id: 3, comment: "testing another comment"} so you can access it by doing item.comment

Try this out:

<div>
    <ol>
       <li *ngFor="let item of comments" > 
               <ol>
                  <li > 
                      <div>{{item.comment}}</div> 
                  </li>
               </ol>
        </li>
       </ol>
   </div>
Sign up to request clarification or add additional context in comments.

4 Comments

I feel like this should work but it´s producing a blank, too.
Add a <pre>{{ item | json }}</pre> inside the ngFor and tell me what it prints. If comments is really what you think, it should print: {id: 3, comment: "testing another comment"} and {id: 8, comment: "second comment"}
Your comment sent me on the right path. I did this:
<div *ngFor="let item of gotClient.comments">test {{item.comment}} </div> And it worked!

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.