Samot
Samot

Reputation: 113

Is it correct that one-time-binding re-evaluates variable when combined with ngIf directive? - AngularJS

One of my mates in work show me a really weird behavior of one-time binding in angular.

UseCase:

When you have an element which text is binding by that one-time binding inside block which is conditional by ng-if, then if we change that value, for example adding some letters, and later change the condition of ng-if, and after that the value from one-time binding has been refreshed.

HTML:

  <div ng-if="a" class="blue">{{ ::text }}</div>

It is a kind of bug, or expected behaviour?

Here is an example of what I'm doing: http://codepen.io/samot/pen/rLJAdN

Upvotes: 3

Views: 140

Answers (2)

morels
morels

Reputation: 2105

Works as expected because using ngIf directive the code is completely recompiled. If you use ngShow the behavior changes and code behaves as you are expecting from ngIf. This because code is only hidden and not recompiled, so not causing the re-(first)-evaluation of the variable.

Upvotes: 2

jjmontes
jjmontes

Reputation: 26944

If the condition of ng-if is made false and then true, it will recreate its contents, causing the one-time ng-bind directive to be evaluated again.

The only thing one time binding does is to avoid adding a watch on your expression, but it doesn't "cache" or "store" the result for the case the content of a directive is compiled again.

So it's expected behavior.

Upvotes: 7

Related Questions