Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the ternary operator be executed on every digest?

If I use the ternary operator in my Angular.js view will it be executed on every digest(like functions) or only if the variables necessary for the decision are changed?

Example:

<div>{{ui.isTrue ? "foo" : "bar"}}</div>

or:

<div ng-bind="ui.isTrue ? 'foo' : 'bar'"></div>

Would it be executed on every digest or only when is ui.IsTrue changed?

like image 673
Tom Avatar asked Feb 05 '23 03:02

Tom


1 Answers

In AngularJS, every expression including the ternary operator will be executed:

  • First when the page is loaded.
  • And whenever the ui.isTrue variable is changed in the angular app scope.

If you take a look at angular scope documentation and specifically Scope as Data-Model section, you will see that:

Scope is the glue between application controller and the view. During the template linking phase the directives set up $watch expressions on the scope.

The $watch allows the directives to be notified of property changes, which allows the directive to render the updated value to the DOM.

So the view will be always notified when a property in the scope changes, so the ternary expression will be automatically evaluated.

like image 158
cнŝdk Avatar answered Feb 16 '23 03:02

cнŝdk