ng-show not working even though condition is met
Asked Answered
B

1

0

I am trying to use ng-show and it is simply when something exists, display it.

<span ng-show="comment">{{comment}}</span>

I tested this in the scope comment="No" but it hides it. When comment="Yes" it displays it, I am confused why this is happening because in JavaScript I try if (comment) and it works...

Braunschweig answered 19/7, 2013 at 12:10 Comment(0)
D
5

ng-show directive internally uses a method toBoolean

Here is how it looks like

function toBoolean(value) {
  if (value && value.length !== 0) {
    var v = lowercase("" + value);
    value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]');
  } else {
    value = false;
  }
  return value;
}

If you look at the implementation, anything like no, false,n,0 evaluate to false.

Disapprove answered 19/7, 2013 at 12:15 Comment(4)
Ahh you are kidding me.... Okay I figured it was something like that. Do you have a suggestion around this? Like would this work ng-show"comment=="No"?Braunschweig
Why would you ever want 'n', 'no', and 'f' to be falsey? Seems to add confusion and I can't see it being useful...Rightminded
Yea I don't know why that's built in. What other language even evaluates no to be false?? Oh well. I just did comment != null. Thanks guys!Braunschweig
I am not kidding as i have pasted it directly from source code. Advantage of JavaScript framework :). Coming to the point well angular here is helping. Otherwise ng-show should be provided a boolean expression.Disapprove

© 2022 - 2024 — McMap. All rights reserved.