I have two attribute in lightning component. one is List and second is Integer attribute and I want to pass dynamic value of list.

< aura:attribute name = "index" type ="Integer" />

< aura:attribute name = "DataList" type ="List" />  

< c:myChild_CMP obj="{!v.DataList[index]}" /> 

//getting Error here

You can't access a dynamic element this way. Instead, you could write another attribute and use that as the value to bind to:

<aura:attribute name="indexData" type="Object" />
<aura:handler name="change" value="{!v.index}" action="{!updateIndexData}" />
<aura:handler name="change" value="{!v.DataList}" action="{!updateIndexData}" />
<c:myChild_CMP obj="{!v.indexData}" />

updateIndexData: function(component, event, helper) {
    var dataList = component.get("v.DataList"),
        index = component.get("v.index"),
        data = dataList && index >= 0? dataList[index]: null;
    component.set("v.indexData", data);

| improve this answer | |
  • I want to get dynamic value of DataList on component level, because I have some input data which I have to store in the DataList. Is there any way to access the DataList directly in component using index ? – ojuswa Jul 15 '19 at 8:38
  • @user2202077 Yes, but you cannot. You will need to use either aura:iteration, an attribute as shown here, or something else. – sfdcfox Jul 15 '19 at 8:41
  • @user2202077 As long as they're all in the same namespace, this solution should work, I believe, and other solutions, such as using aura:iteration, should also work. Or, come up with an entirely different solution. The point is, you can't access an index dynamically. Your question is itself an x-y problem. Accessing a dynamic index directly is impossible. – sfdcfox Jul 15 '19 at 9:07

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.