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 = [];
}
};
はい。私はそれが私が望むものだと思う。しかし、オブジェクトではなく単純な配列で行う方法を教えてください。そして値をコンソールに出力してください。ありがとう。 –
あなたは正しい答えが名前の配列として欲しいと思います。それは$ scope.selectedStudentsA1を取るため、それは名前が –
の正しい答えの配列を持つでしょう。私は対応する生徒の名前で正解と誤った答えを得て、それをデータベースに送ることができます。私が悩んでいるところは、 'Akhilesh'と' Mandar'を 'Akhilesh、3,2'と'(Mandar、1,3) 'という値で選択した場合です(ほんの一例)。そのフォームでデータを取得する方法。私はあまりにもあいまいではないと思います。 –