0

According to the laws of conditional probability, the statement

S1 && S2

will never be true if S1 is false. However, in angular, this is not alaways true.

In my HTML I have this Angular expression:

ng-if="file.nodes"

file is an object that sometimes has the node property, and if it does, then it is an array. The Angular expression "file.nodes" evaluates to false if the array exists but it is empty (this is already problematic as JS evaluates empty arrays as true for if statements). However:

ng-if="file.nodes && (file.nodes.length == 0 || true)"

WILL be evaluated to true even if the array is empty. In this situation, Angular seems to not only be inconsistent with JS, but also with itself (or the laws of conditional probability). Am I correct in believing this is a bug? Since it seems to be fairly egregious if it is.

6
  • 2
    "Angular expression "file.nodes" evaluates to false if the array exists but it is empty". Noway, it's not possible. Do you have a prove? I have a prove of that empty array is truthy plnkr.co/edit/QODEboTvhuBU3vj2TPu4?p=preview
    – dfsq
    Commented Apr 9, 2015 at 21:09
  • and file.nodes exists, so it will be true, right?
    – batmaniac7
    Commented Apr 9, 2015 at 21:10
  • depends on how they parse the expressions.
    – reptilicus
    Commented Apr 9, 2015 at 21:14
  • 1
    The behaviour is different between angular 1.2 and 1.3. Update to angular 1.3+ and you'll find that this issue has been resolved. Commented Apr 9, 2015 at 21:32
  • I realized right after I posted that the version of Angular I have to use for work is fairly old. I'll make a JSFiddle tomorrow with the appropriate version and make sure everything else is the same as my code. The one thing I am certain about is that adding " && (some other condition)" to the end of an ng-if somehow caused it to evaluate to true in situations where it previously did not. Commented Apr 9, 2015 at 21:32

2 Answers 2

3

Some things we know 1) In JS an empty array evaluates to "true" 2) An || will evaluate to true if either side of it evaluates to true 3) An && requires both the left and right side to evaluate to true for itself to evaluate to true

file.nodes = []

file.nodes && (file.nodes.length == 0 || true) // true

because in JS an empty array evaluates to true (left side is true, now we only need the right), and the right side looks inside the parens and sees that the length is zero so it doesnt even need to check the right because it evaluates to true if the left side is true. now the && has found true on the left and on the right.

//Next example
file.nodes = [1,2,3]
file.nodes && (file.nodes.length == 0 || true)

left side returns true because JS returns true for a non-empty array, right side looks inside the parens and see that the left expression is false, so then it checks the right side of it's ||, which is true, so now you have true && true.

Looks like your ng-if will never return false if you files.nodes has an array inside it =)

http://plnkr.co/edit/r2JO779Q0EVmpNyNKMjD?p=preview

3
  • It's like you didn't even read the question... (heh, I do that too sometimes) Commented Apr 9, 2015 at 21:42
  • 1
    I think @Brian's answer is valid and according to the question asked.
    – SSC
    Commented Apr 9, 2015 at 21:52
  • As I stated in the question, Angular evaluated an existing but empty array as false. This was an error in 1.2 which they fixed in 1.3. Commented Apr 10, 2015 at 15:28
0

To anyone else with the same problem: This is a problem with Angular 1.2.x. Update to to 1.3+ and it will be fixed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.