Denis Pshenov
Denis Pshenov

Reputation: 11327

Does AngularJS reuse watchers?

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

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

In this example, does AngularJS reuse watchers for items, item and item.name or does it register new watchers even if it's actually "physically" the same object or property?

Upvotes: 3

Views: 94

Answers (1)

Chad Robinson
Chad Robinson

Reputation: 4623

It will register new watchers. However, if you have reason to know that these values will not be changing once they're set you can help inform AngularJS of this with a new option in 1.3.x (1.3.14 and higher, I think):

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

This will create a short-lived watcher that waits until the value of item.name "settles" (becomes a real value and doesn't change for one digest). After that you won't have a watcher anymore. This is great for things like message lists that aren't going to change unless you yourself trigger it by adding/removing items.

Upvotes: 1

Related Questions