【问题标题】:angularJS nesting ng-repeatangularJS 嵌套 ng-repeat
【发布时间】:2016-01-09 03:57:58
【问题描述】:

我有以下格式的数据。

[
  {
    "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 不显示任何内容。 我也试过 answer[0].name。

【问题讨论】:

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


    【解决方案1】:

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

    td元素不能嵌套,你应该使用不同的元素来显示td里面的内容,比如div或者span

    标记

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

    【讨论】:

    • @Cristian 很高兴为您提供帮助..谢谢 :)
    猜你喜欢
    • 2016-09-13
    • 2014-03-13
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多