1

I am coming back to Angular and have been reading about the digest cycle and watchers, one post I read said that for expressions in {{}} then angular will evaluate this in every cycle.

Given the following will there be 3 watchers set up (qty, cost and for the expression in curly braces)?

<div ng-app ng-init="qty=1;cost=2">
  <b>Invoice:</b>
  <div>
    Quantity: <input type="number" min="0" ng-model="qty">
  </div>
  <div>
    Costs: <input type="number" min="0" ng-model="cost">
  </div>
  <div>
    <b>Total:</b> {{qty * cost | currency}}
  </div>
</div>

The snippet is taken from the angular developer guide, when say the qty variable is increased and the digest fires does it update the model based on a watcher bound to qty and then call another watcher to update the expression in curly braces?

The article said that every time the cycle runs the expression watcher will be fired even if those 2 model variables did not change.

Thanks

berimbolo
  • 3,319
  • 8
  • 43
  • 78

1 Answers1

1

Yes, It’s the $digest cycle where the watchers are fired. When a watcher is fired, AngularJS evaluates the scope model, and if it has changed then the corresponding listener function is called.

locropulenton
  • 4,743
  • 3
  • 32
  • 57
  • Yes I understand that but what I would like to know is if a separate watcher is attached to the expression in curly braces? And if this is fired on every digest loop regardless of whether the variables changed? – berimbolo Nov 24 '16 at 11:00
  • Yes, all watches are fired when $digest is cyling, you could use {{::qty * cost | currency}} in order to bind the watcher once – locropulenton Nov 24 '16 at 15:40
  • Isn't it a drag to run all the expressions even though the values they specifically depend on hasn't changed? if in a big application, if a scope variable changes somewhere deep in code, why should that make every expression re evaluate? shouldn't it be the only ones that depend on that variable? @locropulenton – Manoj Bharadwaj Jun 02 '21 at 08:01