Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular one time binding fails to wait for a boolean condition

The problem:

Using angularjs 1.3.0 beta 15, I'm trying to "bindonce" to a property of an object that is fetched from a server:

<div ng-if="::(myObject.foo === 'bar')"></div>

if myObject is not yet fetched (its promise is not resolved) in the time this html is loaded, angular evaluates the expression as false (no matter the value of course), and stops watching it.

Possible workarounds that I'd rather avoid:

  • Removing the :: solves this of course - not an option (performance-wise)
  • replacing foo with isFooEqualsBar, that gets its value when myObject is resolved (and binding to ::isFooEqualsBar) - it requires a lot of new properties just for that
  • Return to the stable 1.2 version, and use bindonce

Is it a bug? or am I using the :: syntax wrong?

like image 537
seldary Avatar asked May 26 '26 17:05

seldary


1 Answers

One-time expressions will stop recalculating once they are stable, which happens after the first digest if the expression result is a non-undefined value (see value stabilization algorithm below).

I think the problem is that your expression is false after the first digest.

undefined === 'bar' is false.

Maybe something like

<div ng-if="::(myObject.foo ? myObject.foo === 'bar' : undefined)"></div>
like image 181
Karen Zilles Avatar answered May 28 '26 07:05

Karen Zilles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!