How to preserve DOM elements when using ng-repeat with filter?

心已入冬 提交于 2019-12-08 05:40:14

问题


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.

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

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

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

  1. Click the button to apply colors to DOM elements
  2. Type something in the filter input (for example 'ap') to hide some of the elements
  3. Remove the filter. The restored elements lost their style.

回答1:


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?

Add the color directly to the template style="color: {{fruit.color}}"

See this for what I am talking about

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



来源:https://stackoverflow.com/questions/30104976/how-to-preserve-dom-elements-when-using-ng-repeat-with-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!