2

I'm building a lightning component that displays one of many possible modals. I'm using to choose which to display, using an Integer attribute basically as a case.

<aura:attribute name="modalNum" type="Integer" default="0" />
{!v.modalNum} / {!1} / {!v.modalNum==1}
<aura:if isTrue="{!v.modalNum==1}">
    <stuff />
</aura:if>

I feed in the value '1' to modalNum when I create the component. However, the second line renders to "1 / 1 / false", and the content within the doesn't render. What would cause this? My gut says this is some sort of type issue, but I'm not sure how to check for that within lightning code.

  • if you default modalNum to 1, it evaluates to True, correct? where exactly are you assigning your 'modal' attribute values ? – glls Apr 27 '18 at 20:43
  • @glls, defaulting modalNum to 1 still results in the same output. The 'modalNum' originates as an Integer in an Apex controller, which is called from the vf page when it creates this modal component. – iwhitt567 Apr 27 '18 at 20:46
  • Is it '1' or 1? There's a big difference. – Adrian Larson Apr 27 '18 at 22:30
  • @AdrianLarson To which 1 are you referring? The short answer is, none of them are displaying as '1'. – iwhitt567 Apr 27 '18 at 23:18
2

The value was apparent set to the String 1, instead of the number 1. Type coercion isn't as automatic as you'd like to believe in all cases. As a quick fix, consider dividing by one:

<aura:if isTrue="{!(v.modalNum/1)==1}">

I'm not sure why this happens, but you should probably instead just consider using a String instead.

<aura:attribute name="modalNum" type="String" default="0" />
<aura:if isTrue="{!v.modalNum=='1'}">

This would eliminate the need for developers to write odd syntax workarounds elsewhere in the code.

  • Confirmed, dividing modalNum by 1 makes the statement evaluate to true. So strange (and unfortunate) that it gets turned into a string somewhere along the way, but at least I can work with this. Thank you! EDIT: Tried setting the attribute to string, but that also didn't come out to true, so whatever's coming through isn't an integer or a string I guess? – iwhitt567 Apr 27 '18 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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