0

I have a little problem.. I've got this JSON data:

[
{
    "students": {
        "student_id": "2",
        "student_school": "1",
        "student_name": "Charles"            
    },
    "parents": [
        {
            "parent_id": "2",
            "parent_school": "1",
            "parent_name": "Tim"
        }
    ]
},
{
    "students": {
        "student_id": "3",
        "student_school": "1",
        "student_name": "Johnny"
    },
    "parents": [
        {
            "parent_id": "3",
            "parent_school": "1",
            "parent_name": "Kate"
        }
    ]
}
]

The problem is that I try to call to my html page by angular:

{{student.student.student_name}}

Yeah it works but when I want to call the parents data it doesn´t...

{{student.parents.parent_name}}
2
  • 3
    add the code that doesn't work, your attempt to call the parents Commented Apr 21, 2015 at 22:28
  • 1
    How could that code possibly work for that Json? Commented Apr 21, 2015 at 22:30

4 Answers 4

2

Simply:

<div ng-repeat="student in data">
    {{student.students.student_name}}
    {{student.parents[0].parent_name}}
</div>

Or define function in scope called for example getParentDescription and than

<div ng-repeat="student in data">
    {{student.students.student_name}}
    {{getParentDescription(student)}}
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

Because parents is an array. You must specify the index (0 in your case). See the response here : How to get value from a nested JSON array in AngularJS template?

Comments

0

You can't access child scopes directly from parents. See the comment by Vittorio suggesting<ng-repeat="child in parent.children"/> also Binding to Primitives

1 Comment

Why do you think he has multiple scopes? But yeah, we can only guess without seeing any code.
0

I'm guessing student is from an ng-repeat where you go through each object in the array.

Take a closer look at your JSON. While "students": {} points to an object, "parents": [] points to an array. Fix your JSON and it'll be fine

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.