ng-repeat 的简单一种方法绑定? [英] Simple One way binding for ng-repeat?
问题描述
我读过一些文章说如果超过 2000 个项目,ng-repeat 会导致性能不佳,因为有太多的双向绑定要观看.我是 angularjs 的新手,无法理解 ng-repeat 和双向绑定之间的关系:
I have read some articles that said ng-repeat would led to poor performance if there is over 2000 items, because there are too many two way binding to watch. I am new to angularjs and have trouble understanding the relationship between ng-repeat and two-way binding:
ng-repeat(比如输出一个json对象列表)一定要创建双向绑定吗?
Does ng-repeat (like outputting a list of json objects) necessarily create two way binding?
是否有一种简单的方法可以仅使用一种方式绑定来执行 ng-repeat?(最好不需要外部模块)
Is there a simple way to do ng-repeat using only one way binding? (preferably do not need external module)
推荐答案
就像 user1843640 提到的,如果你使用的是 Angular 1.3,你可以使用一次性绑定,但是,为了清楚起见,你需要把 ::在所有绑定上,而不仅仅是中继器.文档说这样做:
Like user1843640 mentioned, if you are on Angular 1.3, you can use one-time-binding, but, just for clarity, you need to put the :: on all the bindings, not just the repeater. The docs say do this:
<div ng-repeat="item in ::items">{{item.name}}</div>
但是,如果我算上观察者,这只会删除一个.要真正减少双向绑定的数量,请将 :: 放在转发器内的绑定上,如下所示:
But, if I count the watchers, this only removed one. To really drop the number of two-way-bindings, place the :: on the bindings within the repeater, like this:
<div ng-repeat="item in ::items">{{::item.name}}</div>
这里有两个 plunker 将显示观察者的数量:
Here are two plunkers that will display the number of watchers:
感谢 Miraage 提供计数观察者的函数 https://stackoverflow.com/a/23470578/2200446
Thanks goes out to Miraage for provinding the function to count the watchers https://stackoverflow.com/a/23470578/2200446
这篇关于ng-repeat 的简单一种方法绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!