使用带过滤器的 ng-repeat 时如何保留 DOM 元素? [英] How to preserve DOM elements when using ng-repeat with filter?

查看:17
本文介绍了使用带过滤器的 ng-repeat 时如何保留 DOM 元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有过滤器的 ng-repeat.当某些项目被过滤器过滤掉,然后在另一个过滤器更改后恢复时,就会为这些项目创建新的 DOM 元素.如果对项目进行了任何 DOM 操作,它会在项目被过滤器隐藏和恢复后丢失.

I have an ng-repeat with a filter. When some items are filtered out by the filter and then they are restored after another filter change there are new DOM elements created for these items. If there was any DOM manipulation on the item it gets lost after item is hidden and restored with filter.

有没有办法保留 DOM 元素,即使项目被过滤器移除?

Is there a way to keep the DOM elements, even when item is removed by filter?

我尝试使用 track by,但没有用.

I tried using track by, but it doesn't help.

这是一个重现问题的 fiddle.重新创建的步骤:

Here is a fiddle that recreates the problem. Steps to recreate:

  1. 单击按钮将颜色应用到 DOM 元素
  2. 在过滤器输入中输入一些东西(例如ap")以隐藏一些元素
  3. 取下过滤器.修复后的元素失去了风格.

推荐答案

Angualr 正在动态添加和删除这些模板.我所说的模板是指你的 ng-repeat 中的任何标签.没有办法在 ng-repeat 中保留它.相反,如果您希望保留此颜色更改,则它需要成为您正在重复的模型的一部分.有意义吗?

Angualr is dynamically adding and removing those templates. By template I mean whatever tag(s) are inside your ng-repeat. There is no way to preserve that in an ng-repeat. Instead, if you want this color change to be preserved, it needs to be a part of the model you are ng-repeat ing over. Does that make sense?

直接在模板中添加颜色style="color: {{fruit.color}}"

看到这里我在说什么

http://jsfiddle.net/nferjvsp/1/

这篇关于使用带过滤器的 ng-repeat 时如何保留 DOM 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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