Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS 1.3+ One time binding for ng-bind with a ternary condition

Would this be correct if I need to use one time binding for a ternary condition inside data-ng-bind directive?

<span data-ng-bind="::model.boolean ? 'json.item.value1' : 'json.item.value2'"></span>

or

<span data-ng-bind="::(model.boolean ? 'json.item.value1' : 'json.item.value2')"></span>
like image 584
dktrvamp Avatar asked Dec 25 '22 19:12

dktrvamp


1 Answers

Yes. The whole expression, whatever it is, will be parsed and read once.

What will happen internally would be equivalent to:

// If not bound
value = $parse("model.boolean ? 'json.item.value1' : 'json.item.value2'")(scope)

Note: If model.boolean is true, you will actually see the string "json.item.value1" and not the real value it contains. If you want to evaluate that, you need to remove the single quotes ' so it becomes:

<span data-ng-bind="::model.boolean ? json.item.value1 : json.item.value2"></span>
like image 128
floribon Avatar answered Dec 29 '22 07:12

floribon