0

I have an object named studentData which has a number of attributes. I have a table corresponding to a certain number of questions. What I intend to do is, for each student, check the value of question number which is correct and push into studentData.correctQuestionNumbers which is an array. However, the checkboxes don't seem to repeat.

View:

<table class="flat-table flat-table-3">
    <tr>
        <th>Student Name</th>
        <th ng-repeat="questionNumber in questionNumbers">{{ questionNumber }}</th>         
    </tr>
    <tr ng-repeat="student in studentData">
        <td>{{ student.name }}</td>
        <td ng-repeat="questionNumber in questionNumbers"><input type="checkbox" ng-model="student.correctQuestionNumbers[]"></td>
    </tr>
</table>

Controller:

$scope.questionNumbers = [1, 2, 3, 4, 5]
    $scope.correctQuestionNumbers = [];

$scope.selectStudents = [{batch:'A1', name: 'ABC'}, {batch:'A2', name: 'XYZ'}];

$scope.studentData = [];
for(var i = 0; i < $scope.selectedStudents.length; i++){
  $scope.studentData.push({name: $scope.selectedStudents[i].name, 
    batch: $scope.selectedStudents[i].batch, 
    correctQuestionNumbers: $scope.correctQuestionNumbers});
}

1 Answer 1

1

Try the following code:

Controller:

$scope.questionNumbers = [1, 2, 3, 4, 5]
$scope.selectedStudents = [{batch:'A1', name: 'ABC'}, {batch:'A2', name: 'XYZ'}];

$scope.studentData = [];
for(var i = 0; i < $scope.selectedStudents.length; i++){
  $scope.studentData.push({name: $scope.selectedStudents[i].name, 
    batch: $scope.selectedStudents[i].batch, 
    correctQuestionNumbers: []});
}

View:

<table class="flat-table flat-table-3">
  <tr>
    <th>Student Name</th>
    <th ng-repeat="questionNumber in questionNumbers">{{ questionNumber }}</th>         
  </tr>
  <tr ng-repeat="student in studentData">
    <td>{{ student.name }}</td>
    <td ng-repeat="questionNumber in questionNumbers"><input type="checkbox" ng-model="student.correctQuestionNumbers[$index]"></td>
  </tr>
</table>
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much. Could you please tell why this works?
But @HARI I cant update the correctQuestionNumbers of both students differenty. It seems they are both same everytime.
is the check box is checked for both students ?
Yes, when I click for one it gets updated for both for same values. But I intend to change it differently for different students. The students wont have same question numbers correct.
did you modified your controller correctQuestionNumbers: [] while pushing student data $scope.studentData.push
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.