Angularjs ng-show directive interprets the 'N' and 'NO' as falsy values
Asked Answered
C

1

6

It seems that Angularjs ng-show directive interprets the 'N' and 'NO' as falsy values.

In my angular application, I'm displaying data related to a specific country using the following <div ng-show="countryCode">some code</div> I was surprised when I figured out that data related to Norway are not shown. And this is because the country code of Norway is 'NO' which is considered as falsy value !!

I don't know if this is a design choice. but if Yes how you deal with this kind of issues

You can reproduce this here

Thank you in advance

Cranage answered 28/10, 2013 at 13:30 Comment(1)
Actually it's a design choice, you can see the responsible function here : github.com/angular/angular.js/blob/…Waldner
D
3

Use ng-show="!!countryCode" to force the behaviour you want.


From reading the code, the behaviour appears to be by design. However, the documentation says:

if the expression is truthy then the element is shown or hidden respectively

which is only true if the angular developers are using truthy to mean something different to its javascript meaning.

Doubt answered 28/10, 2013 at 13:41 Comment(5)
bang, bang. I love JS. :)Gitt
Could use ng-hide="!countryCode" as well.Gitt
!! is an accepted idiom to get a boolean representing the truthiness of a javascript value. Good old dynamically typed languages.Doubt
Yes, I think it's fine. If I came across code like that though I might not know why it was done that way. Might need a comment added for clarity.Gitt
@JoeGauterin nice trick from logic. indeed booleanValue=!(!booleanValue) Thank youCranage

© 2022 - 2024 — McMap. All rights reserved.