NG-重复不更新轨道模型 [英] ng-repeat not update model with track by

查看:166
本文介绍了NG-重复不更新轨道模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用核对表,模型的检验盒的数组,每个选择了一个可以删除工作。一切似乎直到我用它NG重复里面做工精细。

I've been using checklist-model to work with an array of check-boxes with each selected one can be deleted. Everything seems to work fine until I use it inside a ng-repeat.

问题是,被删除的复选框依然存在,当我通过$索引添加跟踪随着 NG-重复。如果我删除的轨道,它工作正常(但我真正的程序,我需要一个轨工作)

The problem is, the deleted check-boxes still be there around when I add the track by $index along with the ng-repeat. If I remove that track by, it works fine (but in my real app I need that track by to work).

下面是一个plnkr,演示

Here's a plnkr, Demo

要看到这个问题,你可以按照以下步骤。

To see the problem, you can follow these steps.


  1. 选择您想要的任何复选框

  2. 删除这些选定的

  3. 检查检查所有的按钮来查看,

现在来看看由一个与轨道,它会留下一些复选框选中。如果手动检查它,它会添加到列表中的旧值。这是奇怪的。

Now look at the one with track by, it will leave some check-box unchecked. If you check it manually, it will have the old value added to the list. This is weird.

任何帮助或交代,才是真正的AP preciate,谢谢

Any help or explaination will be really appreciate, thanks

推荐答案

如果你仍然需要通过跟踪,与对象的id使用它。假设该对象的ID将总是唯一的。

If you still need to track by, use it with the id of the object. Assuming the id of the object will be always unique.

这是具有一个以上的跟踪功能,以解决对相同的键错误。 (这将意味着,两个不同的对象被映射到相同的DOM元素,这是不可能的。)

It is an error to have more than one tracking function to resolve to the same key. (This would mean that two distinct objects are mapped to the same DOM element, which is not possible.)

因此​​,而不是这样的:

So instead of this:

<tr ng-repeat="verb in verbs track by $index">
    <td>
      <input type="checkbox" checklist-model="list.verbs" checklist-value="verb.id">
    </td>
    <td>
      {{verb.id}}
    </td>
    <td>
      <span>{{verb.text}}</span>
    </td>
  </tr>

使用:

  <tr ng-repeat="verb in verbs track by verb.id">
    <td>
      <input type="checkbox" checklist-model="list.verbs" checklist-value="verb.id">
    </td>
    <td>
      {{verb.id}}
    </td>
    <td>
      <span>{{verb.text}}</span>
    </td>
  </tr>

http://plnkr.co/edit/UTtQQJIbtRPdGh0YhRMH?p=$p$ PVIEW

这篇关于NG-重复不更新轨道模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆