4

I've created a simple lightning app which has an aura:attribute of type Object[] with 3 objects in it. I'm binding their selected property to a lightning:input of type checkbox. When I update the value of any of these object properties and print them within the aura:iteration, the values update as and when I check/uncheck my checkbox. But these new values do not get reflected if I try to print them again in another aura:iteration.

Here's the sample app code I've used to try to reproduce this problem:

<aura:application extends="force:slds">
    <aura:attribute name="val" type="Object[]" default="[{name: 'First', selected: true}, {name: 'Second', selected: false}, {name: 'Third', selected: true}]" />
    <aura:iteration items="{!v.val}" var="i">
    <lightning:input type="checkbox" value="test" label="{!i.name}" checked="{!i.selected}" />
        <div>
            {!i.selected}
        </div>
    </aura:iteration>
   <div> 
   <aura:iteration items="{!v.val}" var="j">
       <div>
           {!j.name + ': ' + j.selected}
       </div>
   </aura:iteration>
 </div>     
</aura:application>

Here's how the app looks when the page loads: enter image description here

and here's how the app looks after I check the second checkbox (notice how Second becomes true in the above section but stays false in the below section, even if they're referring to the same attribute.

enter image description here

1

It has to with how aura:valueChange works. It can't detect attributes on an object changing, only the object itself. One simple way to get this to work is to force an aura:valueChange event directly:

<lightning:input onchange="{!c.handleChange}" type="checkbox" value="test" label="{!i.name}" checked="{!i.selected}" />

 handleChange: function(component, event, helper) {
   component.set("v.val", component.get("v.val"));
 }

This will cause the rest of the component to see the new data.

  • Hey, this works, thanks. But this seems like an issue with aura:iteration since as a developer, I would expect this to work without this workaround. Or do you feel otherwise? – Chirag Bhatia - chirag64 Jun 20 '18 at 7:53
  • @ChiragBhatia-chirag64 No, I wish it worked as we expected. However, it doesn't, so for now, this will get you where you need to go. – sfdcfox Jun 20 '18 at 12:04

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.