AngularJS:从表中的复选框重复和获取值

时间:2017-07-02 03:36:48

标签: angularjs

我有一个名为studentData的对象,它有许多属性。我有一张表对应一定数量的问题。我打算做的是,对于每个学生,检查正确的问题编号的值,并推入studentData.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[]"></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 个答案:

答案 0 :(得分: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>