【问题标题】:$index does not exist in ng-repeat - the answer is inside my original post$index 在 ng-repeat 中不存在 - 答案在我原来的帖子中
【发布时间】: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


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

    更新

    我认为问题在于,当您在当前正在查看单个 employee 的循环内引用 employees 时

    正如我所提到的,我从未使用过 $index,所以我不确定,但我也会仔细检查您是否需要在其中引用它(因为您的 WORKING 代码根本没有引用它)

    在您的代码中尝试更改:

    <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="employee.result.editProfile"></td>
    

    如果它确实需要被引用,我会想象引用看起来更像:

    <input type="checkbox" ng-model="employee.result[$index].editProfile">
    

    因为索引通常被引用。

    【讨论】:

    • 好吧,当我转储它时(正如你在我的帖子中看到的那样)它说 array(3).. 我只是不明白为什么这不是一个数组
    • 在您更新代码之前,我写了这个(或正在写它)。所以它是基于你原来的帖子。
    • 哦,我明白了,还有其他基于当前想法的想法吗?我能想到的唯一可能使它不是数组的事情是当我 json_encode 在它被分配给 JS 之前对其进行编码
    • 那么ng-model="employees.result.$index.editProfile"等的值没有设置的问题吗?基本上,您需要更清楚自己正在尝试做什么,或期望发生什么。
    • 还因为您正在循环遍历数组,并且您已将每个索引定义为 employee 可能尝试 employee.result.$index.editProfile 或 ng-model="employee.result.editProfile"&gt; - 换句话说,删除 s 因为那是引用整个数组,而不是您当前所在的单个项目
    猜你喜欢
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多