简体   繁体   English

angularjs中ng-repeat和collection-repeat的区别?

[英]Difference between ng-repeat and collection-repeat in angularjs?

i got a little bit confusion about ng-repeat and collection-repeat我对 ng-repeat 和 collection-repeat 有点困惑

ng-repeat ng-重复

<ion-item ng-repeat="item in items" >
    ...
</ion-item>

collection-repeat收集重复

<ion-item collection-repeat="item in items">
    ...
</ion-item>

the above both lines are doing well and same.以上两条线都做得很好并且相同。

Please let me know if any difference there.请让我知道是否有任何区别。 also i have 1000 items, so which one is better for this scenario?我也有 1000 件物品,那么哪一件更适合这种情况? and how's the rendering speed and what about the performance status ?渲染速度如何?性能状态如何?

collection-repeat is not part of angularjs. collection-repeat不是 angularjs 的一部分。 As far as I can see it's coming from Ionic Framework .据我所知,它来自Ionic Framework As documentation says collection-repeat was created to be used instead of ng-repeat as it might work better with large number of items.正如 文档所说,创建collection-repeat是为了代替ng-repeat ,因为它可能更好地处理大量项目。

collection-repeat allows an app to show huge lists of items much more performantly than ng-repeat collection-repeat允许应用程序以比ng-repeat更高的性能显示大量项目列表

Collection repeat is Ionic's buttery-smooth solution for scrolling huge lists. Collection repeat 是 Ionic 用于滚动大量列表的黄油般流畅的解决方案。 collection-repeat can be used for large list of items and it has high performance when process huge data set.It renders into the DOM only as many items as are currently visible.This means that on a phone screen that can fit eight items, only the eight items matching the current scroll position will be rendered. collection-repeat 可用于大型项目列表,在处理大量数据集时具有高性能。它仅将当前可见的项目呈现到 DOM 中。这意味着在可以容纳八个项目的手机屏幕上,只有将呈现与当前滚动位置匹配的八个项目。

ngRepeat part of the angular Core module. ngRepeat 角核心模块的一部分。 The ngRepeat directive instantiates a template once per item from a collection. ngRepeat 指令为集合中的每个项目实例化一次模板。 Each template instance gets its own scope, where the given loop variable is set to the current collection item, and $index is set to the item index or key.每个模板实例都有自己的范围,其中给定的循环变量设置为当前集合项,$index 设置为项索引或键。

Based on the use case you can use any of the options.根据用例,您可以使用任何选项。 However, if you use ionic framework for mobile then I would suggest to go for Collection repeat.但是,如果您使用 ionic 移动框架,那么我建议您使用 Collection 重复。

Further, If you need to use ng-repeat, it is recommend to process data in the server side and request part by part.另外,如果需要使用 ng-repeat,建议在服务端处理数据,分部分请求。 This way you can expect high performance and less processing in the client side.通过这种方式,您可以在客户端获得高性能和更少的处理。 Another option is store the large data set in the client browser and then process data using that data set.另一种选择是将大型数据集存储在客户端浏览器中,然后使用该数据集处理数据。

In case of 1000 elements I would strongly recommend the collection-repeat .如果有 1000 个elements ,我强烈推荐collection-repeat It's more flexible, and it renders as many items from collection as many can fit the screen .它更灵活,它可以从collection中呈现尽可能多的items适应屏幕 the rest is loaded dynamically, you can even use 200k collection and it will go smooth as well.其余的都是动态加载的,你甚至可以使用 200k 集合,它也会很顺利。 ng-repeat renders the collection items until it's done, so it will give you much worse performance. ng-repeat会渲染collection项,直到完成,所以它会给你带来更糟糕的性能。

So collection-repeat it is my friend.所以collection-repeat它是我的朋友。

Good luck with your development!祝您发展顺利!


Note: collection-repeat is not a part of angular core, look here for more : http://ionicframework.com/docs/api/directive/collectionRepeat/注意: collection-repeat不是角核心的一部分,请在此处查看更多信息:http: //ionicframework.com/docs/api/directive/collectionRepeat/

From what I'm understand is ng-repeat is suitable to use for small numbers of item and collection-repeat is very suitable to use for huge numbers of item.据我了解,ng-repeat 适合用于少量项目,而 collection-repeat 非常适合用于大量项目。 Its actually about the performance when it comes to view/render in a device.它实际上是关于在设备中查看/渲染时的性能。 For more understanding you can see this video , it really help me to understand both of it.为了更多的理解你可以看这个视频,它真的帮助我理解了这两个。

In Simple Word - Ionic has a collection-repeat directive that you can use, instead of ng-repeat, when you need to display very large lists. In Simple Word - Ionic 有一个 collection-repeat 指令,当您需要显示非常大的列表时,您可以使用它来代替 ng-repeat。

How it works?(Deep)它是如何工作的?(深)

The module is divided into two components, $repeatFactory (a service where helper functions are defined) and collectRepeat (a directive where most of the logic lives).该模块分为两个组件,$repeatFactory(定义了帮助函数的服务)和 collectRepeat(大部分逻辑所在的指令)。

The most important takeaway from the factory is the repeatManager method.工厂最重要的收获是 repeatManager 方法。 It is the contructor that is instantiated at the beginning of the link function of the directive and is responsible for maintaining the state of the scrolling view.它是在指令的链接函数开始时实例化的构造函数,负责维护滚动视图的状态。

On creating a new instance of repeatManager, it's also necessary to generate a new object (or map) to provide as reference for all the items that have already been transcluded, rendered, and whose scope has been updated.在创建 repeatManager 的新实例时,还需要生成一个新对象(或映射),以作为所有已被嵌入、渲染和更新其范围的项目的参考。 The enclosing parent element must also be registered so that we can later change it's height to the height of all the items in the collection.封闭的父元素也必须注册,以便我们以后可以将它的高度更改为集合中所有项目的高度。

Next, a watch is set on the collection with an anonymous function set to be invoked whenever the collection changes.接下来,在集合上设置一个监视,并设置一个匿名函数,以便在集合更改时调用。 This is where the bulk of the logic resides.这是大部分逻辑所在的地方。 It's split into three steps.它分为三个步骤。

  • Transclude and render the first element of the collection so that the height and other properties of each individual item may be registered, the height of the parent div adjusted, and the size of the viewport calculated转换并渲染集合的第一个元素,以便可以注册每个单独项目的高度和其他属性,调整父 div 的高度,并计算视口的大小
  • With the size of the viewport determined, loop over and render the n number of items that will fit inside the viewport (plus a few extra for smoothness).确定视口的大小后,循环并渲染将适合视口的 n 个项目(加上一些额外的平滑度)。
  • Set a 'scroll' event listener on .scroll-content to return the scrollHeight of the first element of the collection and render only the necessary elements.在 .scroll-content 上设置一个 'scroll' 事件侦听器以返回集合的第一个元素的 scrollHeight 并仅呈现必要的元素。

There are a few additional tidbits (also known as comments) available in the code below.下面的代码中还有一些额外的花絮(也称为注释)。 It should help alleviate some confusion if you have trouble following the above synopsis.如果您在遵循上述概要时遇到困难,它应该有助于减轻一些困惑。 The module is also available on github.该模块也可以在 github 上找到。

GitHub GitHub

Differences - Working Example差异 - 工作示例

Code Here代码在这里

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

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