2017-04-08 24 views
0

私はMEAN Stackを使用していて、AngularJSの部分をまだ学習しています。 私は、ユーザーから記入されたフォームを持っています。生徒を選択して配列に格納する一連のチェックボックスがあります。各生徒のために、その生徒の2つの値(正解、誤答)を取るつもりです。これらの2つの入力を2つの変数にモデル化し、コントローラの値を取得するにはどうすればよいですか?AngularJS:同じ変数に複数の値を取得する

問題があるのは、私が3人の生徒を選択すると、3組の正解と誤答があります。それ以外の時間に別の生徒を選んだ場合は、その生徒にどのように回答を対応させるのですか?

View.html:

<label ng-repeat="studentA1 in studentsA1"> 
       <input 
       type="checkbox" 
       ng-model="selectedStudentsA1" 
       ng-checked="existA1(studentA1)" 
       value="studentA1" 
       ng-click="toggleSelectionA1(studentA1)"> 
       {{studentA1}} 
</label> 
{{selectedStudentsA1}} 

<div class="col-md-3"> 
    <h6>Correct Answers</h6> 
    <input type="number" min="0" name="correctAnswers" ng-model="correctAnswers" required> 
</div> 
<div class="col-md-3"> 
    <h6>Incorrect Answers</h6> 
    <input type="number" name="incorrectAnswers" ng-model="incorrectAnswers" min="0" required> 
</div> 

Controller.js:

$scope.studentsA1 = ['Akhilesh', 'Prathamesh', 'Mandar', 'Sunmay']; 

    $scope.selectedStudentsA1 = []; 

    $scope.existA1 = function(item) { 
     return $scope.selectedStudentsA1.indexOf(item) > -1; 
    }; 

    $scope.toggleSelectionA1 = function(item) { 
     var idx = $scope.selectedStudentsA1.indexOf(item); 
     if (idx > -1) { 
      $scope.selectedStudentsA1.splice(idx, 1); 
     } else { 
      $scope.selectedStudentsA1.push(item); 
     } 
    }; 

    $scope.checkAllA1 = function() { 
     if ($scope.selectAllA1) { 
      angular.forEach($scope.studentsA1, function(item) { 
       var idx = $scope.selectedStudentsA1.indexOf(item); 
       if(idx >= 0) { 
        return true; 
       } else { 
        $scope.selectedStudentsA1.push(item); 
       } 
      }); 
     } else { 
      $scope.selectedStudentsA1 = []; 
     } 
    }; 

答えて

3

チェックこれはあなた を助け場合、私はあなたがより探しているなら、彼らに

をコメントしてください、あなた のためのフィドルをしました

側面図

<div ng-app ng-controller="mainController"> 
    <table> 
    <tr> 
     <th> 
     <input type="checkbox" ng-model="selectAll" ng-click="selectAllCheckBox(selectAll)"> 
     </th> 
     <th>Name</th> 
     <th>Correct</th> 
     <th>Wrong</th> 
    </tr> 
    <tr ng-repeat="studentA1 in studentsA1"> 
     <td> 
     <input type="checkbox" ng-model="studentA1.selected"> 
     </td> 
     <td> {{studentA1.Name}}</td> 
     <td> 
     <input type="number" ng-model="studentA1.Correct" /> 
     </td> 
     <td> 
     <input type="number" ng-model="studentA1.Wrong" /> 
     </td>\ 
    </tr> 
    </table> 
    <br> {{selectedStudentsA1}} 
    <a ng-click="selectedStudents()">Submit</a> 
</div> 
コントローラ側の

function mainController($scope) { 
    $scope.studentsA1 = [{ 
    selected: false, 
    Name: 'Akhilesh', 
    Correct: '', 
    Wrong: '' 
    }, { 
    selected: false, 
    Name: 'Prathamesh', 
    Correct: '', 
    Wrong: '' 
    }, { 
    selected: false, 
    Name: 'Mandar', 
    Correct: '', 
    Wrong: '' 
    }, { 
    selected: false, 
    Name: 'Sunmay', 
    Correct: '', 
    Wrong: '' 
    }]; 

    $scope.selectedStudents = function() { 
    $scope.selectedStudentsA1 = $scope.studentsA1.filter(i => i.selected == true); 
    $scope.correctAnswers = $scope.selectedStudentsA1.length; 
    $scope.incorrectAnswers = $scope.studentsA1.length -     $scope.selectedStudentsA1.length; 
    } 
    $scope.selectAllCheckBox = function(value) { 
    $scope.studentsA1.forEach(function(item) { 
     item.selected = value; 
    }); 
    } 

} 

https://jsfiddle.net/athulnair/9r8sbwx4/

+0

はい。私はそれが私が望むものだと思う。しかし、オブジェクトではなく単純な配列で行う方法を教えてください。そして値をコンソールに出力してください。ありがとう。 –

+0

あなたは正しい答えが名前の配列として欲しいと思います。それは$ scope.selectedStudentsA1を取るため、それは名前が –

+0

の正しい答えの配列を持つでしょう。私は対応する生徒の名前で正解と誤った答えを得て、それをデータベースに送ることができます。私が悩んでいるところは、 'Akhilesh'と' Mandar'を 'Akhilesh、3,2'と'(Mandar、1,3) 'という値で選択した場合です(ほんの一例)。そのフォームでデータを取得する方法。私はあまりにもあいまいではないと思います。 –

関連する問題