0

I want to change the value of aura:attribute when a particular condition is met inside an aura:iteration. This is the code:

<aura:component>

     <aura:attribute name="products" type="List" />
     <aura:attribute name="chosenProductName" type="String" />

     <div class="row"> 
            <aura:iteration items="{!v.products}" var="product">
                <div class="column" style="margin-bottom:10px;">
                    <div class="card">
                        <div style="background-color:#5E7BFC; padding:30px;">
                            <div class="headerStyle" style="background-color:#6B84F7;">
                                <span style="font-size:15px; color: white">{!product.productName}</span>
                            </div>
                        </div>
                        <div>
                            <aura:method name="getChosenProductName" action="{!c.getChosenProductName}" description="Sample method with parameters"> 
                            </aura:method>
                        </div>
                    </div>
                </div>
            </aura:iteration>
        </div>

</aura:component>

I want to change the value of chosenProductName inside iteration. And this the code I have tried so far. Call js controller method getChosenProductName using aura:method and update the value of chosenProductName inside that method when the conditions are met. But, when I run this code, I get the following error in the aura:method line:

c:ProductsComponent:91,157: Invalid attribute "name": Source

So, what I have to do to solve this? Either I need a way to call js method inside iteration to call component.set() inside that method, or is there anyway to change the attribute value inside iteration itself without calling js method?

  • aura:method is used to provide a method to parent/related components, not for calling arbitrary code during rendering. You can't set an attribute while rendering, as there's no syntax for this, and doing so would cause an infinite rendering loop anyways. It's not clear what you're trying to do, exactly, but this direction isn't the way you want to go. – sfdcfox May 21 at 14:43
  • I just want to change the value of aura:attribute when a condition is met inside iteration. – Jayesh Babu May 22 at 4:16
  • How is the condition met? – sfdcfox May 22 at 4:20
  • actually, salesforce.stackexchange.com/q/306864/82119 this is my previous question. I wanted to do a aura:if inside the iteration. But, it raised error and changed my plan to check condition inside js method, as mentioned in that question's answer. – Jayesh Babu May 22 at 4:30
  • You're stuck in an X-Y Problem. You haven't explained what you're actually trying to do, but instead trying to get an invalid solution to work. What is your primary objective specifically? – sfdcfox May 22 at 4:35

Your Answer

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

Browse other questions tagged or ask your own question.