angularJS嵌套ng重复

angularJS nesting ng-repeat

本文关键字:重复 ng 嵌套 angularJS      更新时间:2023-09-26

我有以下格式的数据。

[
  {
    "id": "1",
    "quizId": "2",
    "question": "What team is Messi playing ?",
    "quiz": [
      {
        "id": "2",
        "categoryId": "67",
        "name": "Football Quiz",
        "quizsize": "0"
      }
    ],
    "answerswers": [
      {
        "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,然后我也试图显示答案.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="answerswer in question.answers">   
            <td>{{ answer.name}}</td>
        </td>
    </tr>
</table>

第二个ng重复没有显示任何内容。我还试着回答了[0].name.

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

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

标记

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