当我尝试将 $index 绑定(bind)到复选框时,在 ng-repeat 中获取 $index 时遇到问题。首先,我分配一个如下所示的数组:
来自 php 的 var_dump:
array(3) {
[0] => array(9) {
["first_name"] => string(3)
"emp" ["last_name"] => string(5)
"loyee" ["username"] => string(4)
"emp1" ["email"] => string(22)
"blabla@gmail.com" ["id"] => string(3)
"539" ["rights_type"] => string(1)
"3" ["editProfile"] => bool(true)["sendMessage"] => bool(true)["editApps"] => bool(false)
}[1] => array(9) {
["first_name"] => string(3)
"one" ["last_name"] => string(4)
"more" ["username"] => string(4)
"emp2" ["email"] => string(19)
"blabla@gmail.com" ["id"] => string(3)
"540" ["rights_type"] => string(1)
"0" ["editProfile"] => bool(false)["sendMessage"] => bool(false)["editApps"] => bool(false)
}["isEmpty"] => bool(false)
}
我的代码如下所示:
<tr ng-repeat="employee in employees.result track by $index" ng-if="employee.username">
<th class="col-md-1" scope="row">{{employee.username}} - ({{employee.first_name}} {{employee.last_name}})</th>
<td class="col-md-1"><input type="checkbox" ng-model="employees.result.$index.editProfile"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employees.result.$index.sendMessage"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employees.result.$index.editApps"></td>
</tr>
该对象正在附加到 Controller 中的 $scope。重要的是, ng-repeat 确实有效,我得到了名字、用户名等的正确值,只是我无法得到 $index 。 当我尝试打印 {{$index}} 时,我什么也没得到。我尝试从数组中删除“isEmpty”键,但它也没有帮助。有什么想法吗?
最佳答案
Roee-distler最初将他们的解决方案编辑到问题中。
<小时/>我所要做的就是将employees.result更改为employee,并且它实际上更新了employees.result(分别是数组的每个单元格),所以它看起来像:
<tr ng-repeat="employee in employees.result track by $index" ng-if="employee.username">
<th class="col-md-1" scope="row">{{employee.username}} - ({{employee.first_name}} {{employee.last_name}})</th>
<td class="col-md-1"><input type="checkbox" ng-model="employee.editProfile"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employee.sendMessage"></td>
<td class="col-md-1"><input type="checkbox" ng-model="employee.editApps"></td>
</tr>
关于javascript - $index 不存在于 ng-repeat 中 - 答案在我原来的帖子中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39006534/