简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM