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}}
Jhony Blaze
  • 251
  • 1
  • 5
  • 15

4 Answers4

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>
Tomasz Białecki
  • 1,041
  • 6
  • 10
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?

Community
  • 1
  • 1
Spawnrider
  • 1,727
  • 1
  • 19
  • 32
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

Community
  • 1
  • 1
Ben Petersen
  • 471
  • 4
  • 15
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

ErikAGriffin
  • 739
  • 3
  • 10
  • 21