2017-07-02 91 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