Cristian
Cristian

Reputation: 285

angularJS nesting ng-repeat

I have data in the following format.

[
  {
    "id": "1",
    "quizId": "2",
    "question": "What team is Messi playing ?",
    "quiz": [
      {
        "id": "2",
        "categoryId": "67",
        "name": "Football Quiz",
        "quizsize": "0"
      }
    ],
    "answers": [
      {
        "id": "1",
        "questionId": "1",
        "name": "Barcelona",
        "correct": "1"
      },
      {
        "id": "2",
        "questionId": "1",
        "name": "M.City",
        "correct": "0"
      },
      {
        "id": "3",
        "questionId": "1",
        "name": "Real Madrid",
        "correct": "0"
      },
      {
        "id": "4",
        "questionId": "1",
        "name": "Liverpool",
        "correct": "0"
      }
    ]
  }
]

I trying to display it in a table. Each question(the root) is a row, the for one td I get the quiz.name and then I'm trying to display the answers.name as well.

<table class="table table-striped">
    <tr>
        <th>Title</th>
        <th>Category</th>
        <th>Answer 1</th>
        <th>Answer 2</th>
        <th>Answer 3</th>
        <th>Answer 4</th>

    </tr>
    <tr ng-repeat="question in questions"> 
        <td>{{ question.question}}</td>    
        <td>{{ question.quiz[0].name }}</td> 
        <td ng-repeat="answer in question.answers">   
            <td>{{ answer.name}}</td>
        </td>
    </tr>
</table>

The second ng-repeat does not display anything. I also tried answer[0].name.

Upvotes: 0

Views: 831

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

Your current HTML is invalid as per standards.

td element can not be nested, you should use different element to display content inside td like div or span

Markup

<tr ng-repeat="question in questions"> 
    <td>{{ question.question}}</td>    
    <td>{{ question.quiz[0].name }}</td> 
    <td ng-repeat="answer in question.answers">   
        <div>{{ answer.name}}</div>
    </td>
</tr>

Upvotes: 4

Related Questions