angularJS嵌套ng-repeat

时间:2016-01-08 19:46:09

标签: javascript html angularjs angularjs-ng-repeat html-table

我的数据格式如下。

[
  {
    "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"
      }
    ]
  }
]

我试图在表格中显示它。 每个问题(根)都是一行,一个td我得到了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>

第二次ng-repeat不显示任何内容。 我也试过回答[0] .name。

1 个答案:

答案 0 :(得分:4)

根据标准,您当前的HTML无效。

td元素无法嵌套,您应该使用不同的元素来显示td内的内容divspan

<强>标记

<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>