function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alen hilstonAlen hilston 

How to add conditionl statements in Aura:if

Hi,

Please help. I am trying to add some conditional statement like in vfpage on my aura component but I am not able to do it. It keeps showing me the same error. 
User-added image

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute type="Integer" name="score"/>
    
    <aura:if isTrue="{!v.score > 0 && v.score < 10}">
    <b>You are not eligible</b>
    </aura:if>
    
</aura:component>
 

Please let me know if I am doing something wrong!

Best Answer chosen by Alen hilston
Suraj Tripathi 47Suraj Tripathi 47

Yes, you can use the value of one attribute and set the value of another attribute by using below code : 

<aura:attribute name="one" type="String"/> // value which is to be checked

<aura:attribute name="two" type="String" default="{!if(v.one== 'this','raised','not raised')}"/> // attribute whose value is to set default.

Here we have checked the value of one and based on this value we have set the default value of attribute two.

I hope it helped you. It would be nice if you mark it as the best answer.

Thanks.

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi Alen,

 

Hope you are doing well.

 

We cannot add similar conditions in Aura as we do on the VF page. In the Aura component, we have different formats and statements for conditional statements. In the aura component, we use pre-build conditional expressions.

The above condition can be fulfilled by making condition into this format given below:

<aura:if isTrue="{! and( greaterthan(v.score,0), greaterthan(10,v.score) ) }">

For further more information, you can refer to this link given below:

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/expr_conditional.htm

 

Hope this will resolve your answer. 

Please could you mark it as the best answer if it resolves your query?

 

Thanks.

Alen hilstonAlen hilston

Hi Suraj,

Thanks, It worked. Could you please let me know if there's anything i can do to set default values of attribute on the basis of another attribute?

Suraj Tripathi 47Suraj Tripathi 47

Yes, you can use the value of one attribute and set the value of another attribute by using below code : 

<aura:attribute name="one" type="String"/> // value which is to be checked

<aura:attribute name="two" type="String" default="{!if(v.one== 'this','raised','not raised')}"/> // attribute whose value is to set default.

Here we have checked the value of one and based on this value we have set the default value of attribute two.

I hope it helped you. It would be nice if you mark it as the best answer.

Thanks.

This was selected as the best answer
Alen hilstonAlen hilston
Thanks Suraj. It is working now, though I have a boolean field but I managed to make it work.