Simple One way binding for ng-repeat?

て烟熏妆下的殇ゞ 提交于 2019-11-30 04:33:00
Cindy Conway

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>

Here are two plunkers that will display the number of watchers:

All Bindings
Repeater Only

Thanks goes out to Miraage for provinding the function to count the watchers https://stackoverflow.com/a/23470578/2200446

For anyone using or upgrading to Angular 1.3 you can now use "one-time binding". For ng-repeat it would look like this:

<div ng-repeat="item in ::items">{{item.name}}</div>

Notice the ::items syntax.

For more information check the Angular documentation for expressions.

This blog post presents some interesting solutions. The end result was:

Upgrade to AngularJS 1.1.5 and use limitTo together with Infinite scrolling. AngularJS ng-repeat offers from version 1.1.4 the limitTo option. I slightly adapted the Infinite Scroll directive to make scrolling within a container possible that does not have height 100% of window.

Basically you limit the number of objects you initially render, then use the Infinite scrolling directive to render more as needed. Since you don't want an external module, just mimic the infinite scroll functionality as needed with your own script.

Note: This should solve your performance problem but it won't remove two-way binding.

  1. what ever is generated by ng-model will be having a watcher on data(model) which reduces the page performance if it crosses 200 watchers.

  2. Refer this for ONE WAY BINDING http://blog.scalyr.com/2013/10/31/angularjs-1200ms-to-35ms/ but make sure you use it properly

Hope it helps!!!

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