javascript - $index 不存在于 ng-repeat 中 - 答案在我原来的帖子中

标签 javascript php html angularjs

当我尝试将 $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/

相关文章:

javascript - 从 Leaflet map 中删除 geoJSON

javascript - 无法在 karma 中加载 phantomjs 网页和系统模块

php - 如何在用户点击提交按钮时将数据发送回相同的表单

javascript - 如何使用 jquery 使 html anchor 标记不可点击?

javascript - MVC返回多个对象到jquery post数据

php - 更新数据按钮

php - 如何将MySQL日期时间转换为ISO 8601日期格式而不带偏移量?

javascript - 由于调整 svg 元素的大小导致鼠标事件不匹配

html - 与 Django 一起使用时,Bootstrap Navbar 不会变色

html - 两个 div(一个有绝对高度,另一个有滚动条)