使用ng-repeat访问值

时间:2016-11-22 10:45:12

标签: angularjs

我正在尝试使用ng-repeat访问某个值。我需要访问QuestionID的值。我试过了value.QuestionID,但它没有用。

JSON

[
  {
    "QuestionID": 101,
    "Question": "q1",
    "Response": "a1.1",
    "CorrectResponse": false
  },
  {
    "QuestionID": 101,
    "Question": "q1",
    "Response": "a1.2",
    "CorrectResponse": true
  }
]

HTML

<body ng-app="DemoApp" ng-controller="DemoController">
   <ul ng-repeat="(key, value) in data | groupBy: 'Question'">
      Question name: {{ key }}
      <li ng-repeat="answer in value">
         {{ answer.Response }}
      </li>
   </ul>
</body>

2 个答案:

答案 0 :(得分:1)

由于您按&#39;问题&#39;分组,您的问题ID&#39;将在&#34; answer&#34;

<body ng-app="DemoApp" ng-controller="DemoController">
   <ul ng-repeat="(key, value) in data | groupBy: 'Question'">
      Question name: {{ key }}

      <span>{{ value[0].QuestionID }}</span>

      <li ng-repeat="answer in value">
         {{ answer.Response }}
         <!-- {{ answer.QuestionID }} -->
      </li>
   </ul>
</body>

答案 1 :(得分:-2)

给出你的JSON

[{
    "QuestionID":101,
    "Question":"q1",
    "Response":"a1.1",
    "CorrectResponse":false
}, 
{
    "QuestionID":101,
    "Question":"q1",
    "Response":"a1.2",
    "CorrectResponse":true
}]

我无法看到你的第二次重复重复是什么。但无论如何:

<body ng-app="DemoApp" ng-controller="DemoController">
   <ul ng-repeat="value in data">
      Question name: {{ value.Question }} <br/>
      Answer: {{ value.Response }}

   </ul>
</body>

根据您的JSON,这是正确的。