[英]Angularjs form fields in nested ng-repeat
Hi I have nested form field like this 嗨,我有这样的嵌套表格字段
<table>
<tbody ng-repeat="student in students">
<tr>
<td colspan="3">{{student.sname}}</td>
</tr>
<tr>
<td>Subject</td>
<td>External Mark</td>
<td>Internal Mark</td>
</tr>
<tr ng-repeat="subject in exam_subjects">
<td>{{subject.subject_name}}</td>
<td><input ng-model="subject.student.external_mark"></td>
<td><input ng-model="subject.student.internal_mark"></td>
</tr>
</tbody>
</table>
This is for putting exam marks of subjects for students. 这是为学生设置科目的考试成绩。 It is displaying fine but when I put mark to a subject external_mark/internal_mark all the fields of external_mark/internal_mark of that subject get filled the same value. 它显示得很好,但是当我将标记置于主题external_mark / internal_mark时,该主题的external_mark / internal_mark的所有字段都填充相同的值。 How do I handle this? 我该如何处理? Thank you for your any help and suggestions 感谢您的帮助和建议
The input needs a unique name within the form . 输入在表单中需要唯一的名称。
<tr ng-repeat="subject in exam_subjects">
<td>{{subject.subject_name}}</td>
<td><input name="external_{{$index}}" ng-model="subject.student.external_mark"></td>
<td><input name="internal_{{$index}}" ng-model="subject.student.internal_mark"></td>
</tr>
The idea is to map each student's subject wise marks with unique scope variable. 想法是用唯一的范围变量映射每个学生的主题明智的标记。 For each student save an array of external and internal marks. 为每个学生保存一系列外部和内部标记。 I make your students object similar to this: 我让您的学生对此表示反对:
$scope.students=[{
"sname": "abc",
"marks": []
}];
$scope.exam_subjects=[{
"id": "1",
"subject_name": "Physics"
},
{
"id": "2",
"subject_name": "Maths"
}];
In your view: 您认为:
<tr ng-repeat="subject in exam_subjects">
<td>{{subject.subject_name}}</td>
<td><input ng-model="student.marks[{{subject.id-1}}].external_mark"></td>
<td><input ng-model="student.marks[{{subject.id-1}}].internal_mark"></td>
</tr>
The code is not tested. 该代码未经测试。 It is just to give you an idea of how this problem could be solved 只是为了让您了解如何解决此问题
Answer number2. 答案编号2。 Now that I know what your doing (which was difficult from you initial post) I would recommend you something like this. 现在,我知道您的工作了(在您最初的帖子中很难做到),我将向您推荐这样的内容。
NOTE It will only work if you have a dedicated PUT/PATCH function for the single subject. 注意仅当您对单个主题具有专用的PUT / PATCH功能时,此功能才有效。
exam_subjects should be an array. exam_subjects应该是一个数组。 in your controller as $scope.exam_subjects. 在您的控制器中为$ scope.exam_subjects。 When you press edit do it something like this. 当您按编辑时,请执行以下操作。
<tr ng-repeat-start="subject in exam_subjects">
...
<td><button ng-click=onEdit(subject)></button></td>
</tr>
<tr ng-repeat-end="" ng-if="vm.selectedSubject.Id===subject.Id">
<td colspan="">
//your form that you bind to selectedSubject. add submit and Cancel button.
</td>
</tr>
in controller. 在控制器中。
$scope.onEdit=function(subject){
$scope.selectedSubject = angular.copy(subject);
//need to be a copy if you want to be able to Cancel post. Otherwise Angular will update array element directly
}
$scope.onCancel = function();
$scope.selectedSubject='';
}
$scope.onSubmit = function(subject){
//your submit code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.