2017-07-02 93 views
0

我有一个名为studentData的对象,它具有多个属性。我有一个对应于一定数量问题的表格。我打算做的是,为每个学生,检查问题编号的值是否正确,然后推入studentData.correctQuestionNumbers这是一个数组。 但是,复选框似乎没有重复。AngularJS:重复​​并从表格中的复选框中获取值

检视:

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

控制器:

$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

尝试以下代码:

控制器:

$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: []}); 
} 

查看:

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

非常感谢。你能告诉我为什么这个工作吗? –

+0

但@HARI我不能更新两个学生不同的'correctQuestionNumbers'。看来他们每次都是一样的。 –

+0

是否勾选了两个学生的复选框? – HARI