In Visualforce apex:variable can be used to avoid the repetition of a Visualforce expression.

I have a collection returned from an @AuraEnabled method for which I want to do the same sort of thing that apex:variable does:

<aura:iteration var="item" items="{! v.items }">
    <!-- aura:variable does not exist: is there a way to do this in Lightning? -->
    <aura:variable var="sob" value="{! item.reference.sobReference }"/>
        <td>{! sob.Name }</td>
        <td>{! sob.Birthdate }</td>

Is there a way to do this in Lightning Components?



You can use an attribute in the lightning component, and then set the value in your controller.





Edit: It seems like you're just trying to alias an attribute you already have access to.

<aura:iteration var="item" items="{! v.items }">
    <td>{! item.reference.sobReference.Name }</td>
    <td>{! item.reference.sobReference.Birthdate }</td>

  • Ross, I've tried various combinations of aura:attribute and aura:set (to assign the attribute inside the loop - don't see how to use the controller here) but none that I tried would save. Can you post a working example in your answer? – Keith C Mar 29 '17 at 13:52
  • Yes I am trying to avoid the repetition of the common expression .reference.sobReference in the same way you would use a local variable in other programming languages. – Keith C Mar 30 '17 at 7:00

The only pattern I've seen that works is to wrap your iteration in a custom rowComponent where attributes inside the row represent each of your nested expressions (for complicated objects).

So then you pass your sob in as a top level attribute and then break down each nested reference using something like <aura:attribute name="sobName" default="{! v.sob.name }"/>

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.