了解bindonce限制

时间:2014-03-09 10:08:53

标签: angularjs angularjs-directive bindonce

我一直在阅读bindonce作为减少手表和提高性能的方法。为了更好地理解包,我用ng-repeat做了一个例子。

JSBIN here

没有bindonce我有103个手表,100个列表项+ 2个按钮。

使用bindonce我得到3个手表,2个底子+ 1个列表。

如果我正确理解binonce,则一旦绑定对象被解析并渲染,它就会移除手表。所以,

如何使用bindonce,对对象所做的更改仍会反映在DOM中?

1 个答案:

答案 0 :(得分:9)

文档中有一个提示:

Now this example uses 0 watches per person and renders exactly the same result as the above that uses ng-. *(Angular still uses 1 watcher for ngRepeatWatch)

关键是Angular仍然会监视ngRepeat,因此如果数组更改ngRepeat将重新呈现数组并重新应用bindonce功能。

我已更新您的jsbin example here以更好地说明此http://jsbin.com/xugemico/2/edit

请注意以下内容:

<p>
  Bindonce: first item: 
  <span bindonce="arr" bo-bind="arr[0]"></span>
</p>

上面的代码在没有ngRepeat监视的第一个数组项目上使用bindonce,你会看到该值没有根据ngRepeat中的bindonce进行更新。