【发布时间】:2016-08-18 09:28:10
【问题描述】:
尝试将 $index 绑定到复选框时,我在 ng-repeat 中获取它时遇到问题。首先,我分配了一个如下所示的数组:
来自 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>
这个对象被附加到控制器中的 $scope。重要的是说 ng-repeat 确实有效,我得到了正确的名字、用户名等值,这只是我无法得到的 $index 。 当我尝试打印 {{$index}} 时,我什么也得不到。我试图从数组中删除“isEmpty”键,但它也没有帮助。有什么想法吗?
【问题讨论】:
-
$index定义在哪里? -
这不是一个数组:{0:"value1",1:"value2",isEmpty:false}
-
您将需要发布您的实际代码以获得帮助。
employees{0:"value1",1:"value2",isEmpty:false}不是一个数组,它是一个对象。并且数组有括号[ ] -
and employees.result 不会指向任何东西
-
employees.result 似乎也没有定义
标签: javascript php html angularjs