3

I'm trying to update the label for a lightning component based on if a field is populated or not. I've tried a ton of things, and so far nothing is working (only the else statement is appearing):

<aura:attribute name="opportunity" type="Opportunity"/>
<aura:if isTrue="{or(v.opportunity.sample_id__c == null, v.opportunity.sample_id__c == '')}">
    <ui:button label="Create Sample" press="{!c.generateSampleWindow}"/>
<aura:set attribute="else">
    <ui:button label="View Sample" press="{!c.generateSampleWindow}"/>
</aura:set>
</aura:if>

I've also tried simply isTrue="{!v.opportunity.sample_id__c}", but also doesn't work.

Thanks!

10

You are missing the bang (!)

Do it like this:

<aura:if isTrue="{! or(v.opportunity.sample_id__c == null, v.opportunity.sample_id__c == '')}">

Also, to just check that the isTrue statement is set up properly, try defining and new attribute, say testBoolean to verify everything is working as it should (which, as you mentioned in the comments, it is not). Ie:

<aura:attribute name="testBoolean" type="Boolean" default="true"/>
<aura:if isTrue="{!v.testBoolean}">
    <div>It works!!</div>
</aura:if>

Also, as mentioned in other answers, you can simplify your expression a lot and just calculate the value of the label.

Good luck!

  • Unfortunately this did not work, but I appreciate the response (and it seems like it should work... not sure why it isn't). – JimKasper99 Dec 8 '17 at 20:00
  • What you need to do is check for case sensitivity, the name, the data in the name. Use a console print statement or an alert or the Javascript debugger if you can. Basically that syntax is correct, which says that something else is not. – Caspar Harmer Dec 8 '17 at 21:49
7

And instead of using the aura:if you can simply use the ternary inside the label of the button like this and yes you also have to fix the binding issue using !:

<aura:attribute name="opportunity" type="Opportunity"/>

<ui:button 
label="{!(empty(v.opportunity.sample_id__c)? 'Create Sample': 'View Sample')}"
press="{!c.generateSampleWindow}"/>

And to check for null and '', You can use the empty method.

  • Thank you for the response, unfortunately this did not work either. – JimKasper99 Dec 8 '17 at 20:01
  • The syntax is correct, you just need to correct the attribute name. And also do share how you make it work. – itzmukeshy7 Dec 9 '17 at 2:57

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.