0

Well today i have a new problem. I'm trying to take multiple arrays of this json.

[ { "_id" : "541200a79fb6706de9164063" , "index" : 0 , "guid" : "403d5692-0f07-40e6-84f5-32cdee137127" , "isActive" : true , "school" : "BOILICON" , "address" : "532 Everit Street, Williamson, Kentucky, 197" , "registered" : "Monday, March 24, 2014 4:46 AM" , "professor" : [ { "id" : 0 , "name" : { "first" : "Mclean" , "last" : "Robertson"} , "email" : "[email protected]" , "age" : 36 , "phone" : "+1 (946) 436-2567" , "coursePrice" : "$4,882.28" , "favoriteParadigm" : "funcional"} ]}

Well i tried to do it like this on the html code:

$<div ng-controller="studentsController">
    <table>    
        <tr ng-repeat= "student in data.students">
            <td>{{student.index}}</td>
            <td>{{student.guid}}</td>
            <td>{{student.isActive}}</td>
            <td>{{student.school}}</td>
            <td>{{student.address}}</td>
            <td ng-repeat="prof in data.students.professor"> 
                email: {{prof.email}}
            </td>
        </tr>
    </table>
</div>$

But it doesn't work for me. Can anyone help me please?

4
  • the json over here is incorrect. Is that the reason? Commented Sep 12, 2014 at 5:35
  • The json is invalid you've missed that last square bracket Commented Sep 12, 2014 at 5:37
  • Well im not sure this is the json link link Commented Sep 12, 2014 at 5:38
  • that's not the problem i cut the json because the json is so big there is the link jsonPage Commented Sep 12, 2014 at 5:40

3 Answers 3

1

Change prof in data.students.professor to prof in students.professor since previously you have defined student in data.students so that student will contains professor details internally

Try this out

Working Demo

<div ng-app='myApp' ng-controller="studentsController">
    <table border="1">
        <tr ng-repeat="student in students">
            <td>{{student.index}}</td>
            <td>{{student.guid}}</td>
            <td>{{student.isActive}}</td>
            <td>{{student.school}}</td>
            <td>{{student.address}}</td>
            <td ng-repeat="prof in student.professor">email: {{prof.email}}</td>
        </tr>
    </table>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

within ng-repeat you have different scope and current item is directly accessible. So change

<td ng-repeat="prof in data.students.professor">

to this:

<td ng-repeat="prof in student.professor"> 
    email: {{prof.email}}
</td>

Comments

0

Couple of things in the problem statement,

  1. First of all you need to avoid duplicates so have a track by expression in your ng-repeat.
  2. Since you are storing the json in a string you need to parse it to JSON using JSON.parse.
  3. Just like what @Zaheer mentioned since you need to refer to the outside ng-repeat you need to refer to the student instead of prof in data.students.professor

So your code will look like:

HTML:

<div ng-app>
    <table ng-controller="test">
        <tr ng-repeat="student in data.students track by $index">
            <td>{{student.index}}</td>
            <td>{{student.guid}}</td>
            <td>{{student.isActive}}</td>
            <td>{{student.school}}</td>
            <td>{{student.address}}</td>
            <td ng-repeat="prof in student.professor track by $index">email: {{prof.email}}</td>
        </tr>
    </table>
</div>

JS:

function test($scope) {
    $scope.data = {};
    var students = '[ { "_id" : "541200a79fb6706de9164063" , "index" : 0 , "guid" : "403d5692-0f07-40e6-84f5-32cdee137127" , "isActive" : true , "school" : "BOILICON" , "address" : "532 Everit Street, Williamson, Kentucky, 197" , "registered" : "Monday, March 24, 2014 4:46 AM" , "professor" : [ { "id" : 0 , "name" : { "first" : "Mclean" , "last" : "Robertson"} , "email" : "[email protected]" , "age" : 36 , "phone" : "+1 (946) 436-2567" , "coursePrice" : "$4,882.28" , "favoriteParadigm" : "funcional"} ]}]';
    $scope.data.students = JSON.parse(students);
}

Working Fiddle

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.