简体   繁体   English

angularJS嵌套ng-repeat

[英]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. 每个问题(根)都是一行,一开始我得到quiz.name,然后我也尝试显示Answers.name。

<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. 第二个ng-repeat不显示任何内容。 I also tried answer[0].name. 我也尝试了答案[0] .name。

Your current HTML is invalid as per standards. 根据标准,您当前的HTML无效。

td element can not be nested, you should use different element to display content inside td like div or span td元素不能嵌套,您应该使用其他元素在td内显示内容,例如divspan

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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