简体   繁体   English

从ng-repeat中删除元素,但将其HTML保留在dom中

[英]Remove element from ng-repeat but, keep its HTML in dom

I have an array badge = [{'name':'abc', 'flag': true}, {'name':'cde', 'flag': false}, {'name':'def', 'flag': true} ] 我有一个数组badge = [{'name':'abc', 'flag': true}, {'name':'cde', 'flag': false}, {'name':'def', 'flag': true} ]

used it with ng-repeat and dom is created on browser. ng-repeat一起使用,并且dom是在浏览器上创建的。 Now i need to delete element having flag false from array but, keep the HTML (created by ng-repeat) entact in browser. 现在,我需要从数组中删除标记为false元素,但是要保持HTML(由ng-repeat创建)在浏览器中的作用。

Use filter 使用filter

 angular.module('myApp', []); function HelloCntl($scope) { $scope.badge = [{ 'name': 'abc', 'flag': true }, { 'name': 'cde', 'flag': false }, { 'name': 'def', 'flag': true }] } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script> <div ng:app="myApp"> <div ng-controller="HelloCntl"> <ul> <li ng-repeat="b in badge | filter:{flag:true}"> <span>{{b.name}}</span> <span>{{b.flag}}</span> </li> </ul> </div> </div> 

Use one time binding. 使用一次绑定。 So that when you change model then DOM will not be reflected again after first time load. 这样,当您更改模型时,DOM将不会在第一次加载后再次反映出来。

<ul>
      <li ng-repeat="b in ::badge">
        <span>{{b.name}}</span>
        <span>{{b.flag}}</span>
      </li>
</ul>

For disabling the deleted things, use ng-class with normal two way binding. 要禁用删除的内容,请使用具有常规双向绑定的ng-class

For one time binding please google, or refer Do bindings nested inside of a lazy one-time ng-repeat binding bind just once? 对于一次性绑定,请使用google或参阅嵌套在一次懒惰的ng-repeat绑定内的绑定是否仅绑定一次?

In your HTML use something like: 在您的HTML中使用类似:

<ul ng-repeat="(key,value) in badge">
  <li ng-show="value.flag">{{value.name}}</li>
</ul>

Using ng-if will remove the element from the DOM. 使用ng-if会将元素从DOM中删除。 Using ng-show will just hide it from the DOM (using ng-hidden class). 使用ng-show只会将其从DOM中隐藏(使用ng-hidden类)。

You could put two ng-repeats next to each other and have the second one initially empty and your first one as you show it. 您可以将两个ng-repeats相邻放置,并让第二个ng-repeat最初为空,而第一个为空白。 Then you can delete it from the first array and put it in the second one. 然后,您可以从第一个数组中删除它,然后将其放入第二个数组中。

This way you can remove the element, but still keep it in the DOM. 这样,您可以删除元素,但仍将其保留在DOM中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM