3

i'm having a problem to display a element of a JSON in the html

Here is the JSON

peopleList: [
    {
        name: 'John',
        address: 'Street A, 143',
        sons: [{
            First: "Jack",
            Second: "Jane",
            Third: "Maria"
        }]
    }]

And here is the html

<div ng-repeat="people in peopleList">
    <span class="">name: {{people.name}}</span><br>
    <span class="">sons: {{people.sons}}</span>
</div>

And this show Sons: [{"First": "Jack", "Second": "Jane", "Third": "Maria", "_id": "5604e8e474758451s6d813"}]

I have tried to use

people.sons.First

but didn't showed anything

2
  • try this: {{people[0].sons[0].First}} Commented Sep 25, 2015 at 7:05
  • what depends are ng-repeat="name in names" and people?. Or, does they depend? Commented Sep 25, 2015 at 7:07

4 Answers 4

1

Your ng-repeat iteration looks kinda suspicious to me, try below

<div  ng-repeat="name in names">
   <span class="">name: {{name.name}}</span><br>
   <span class="">sons: {{name.sons.First}}</span>

 </div>

If you want to iterate Sons then it may look like below

<div  ng-repeat="name in names">
   <span class="">name: {{name.name}}</span><br>
   <span class="" ng-repeat="son in name.sons">
                 sons First: {{son.First}}
                 sons Second: {{son.Second}}                       
                 sons Third: {{son.Third}}                       
    </span>

 </div>
Sign up to request clarification or add additional context in comments.

2 Comments

I think it should be name in people
Good Point! OP has not shown what scope variable he is using. I was thinking same but though just an idea of iterating object may lead him to find answer.
1

What you need is

{{people.sons[0].First}}

if you need to show just the first son.

This is because your sons property is an array (which is a bit odd - it implies that you have a 2nd set of sons? :-)). The [0] selects the set and the .First selects Jack

3 Comments

I think it should be name in people
@Satpal - thanks! I think OP seemed worried more about the sons part (the result and trials are more around that)
Thanks, it helped a lot
0

use

 <span class="">name: {{name.name}}</span><br>
   <span class="">sons: {{name.sons.First}}</span>

1 Comment

I have tried this and didn't worked, maybe i had made some mistake on the syntax haha
0

You are repeating over names into name, so if you have a json array in names and if you want to render,according to your example please use name.name name.sons in the html inside ng-repeat

1 Comment

Hmmm, Okay, i will try, Thanks!

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.