user6934713 user6934713 - 12 days ago 4
AngularJS Question

Access a value with ng-repeat

I am trying to access a value with ng-repeat. I need to access the value of the 'QuestionID'. I tried value.QuestionID but didn't work.

This is my json:

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


and this is my html code

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

Answer

Since you grouped by 'Question', your 'QuestionID' will be inside "answer"

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