3

I have a "container" component that handles all of the component creation/removals dynamically in my app.

If the attribute sharedAttribute exists on both the parent and child component then using it like this:

<c:childObject sharedAttribute="{!v.sharedAttribute}"/>

should reflect changes on the parent object if the attribute is set on the child object.

However, if I create the child object dynamically like this:

$A.createComponent(
                "c:childObject",
                {
                    "sharedAttribute" : cmp.get("v.sharedAttribute")
                },
                function(newCmp){
                    //do stuff
                }
});

Then changes arent reflected on the parent component when the child component is updated.

Where did I go wrong?

3
  • Please share the full component code showing both parent and child components code Oct 17, 2016 at 16:46
  • This is enough information to answer the question. Create any variant of this and you will see the same results. I appreciate the help but adding additional code would add to the confusion. Oct 17, 2016 at 17:35
  • It's not enough info since I need to see how you append the child to the parent component and how you are debugging the parent attribute. Oct 17, 2016 at 17:37

1 Answer 1

5

After some more digging I found out .getReference("v.sharedAttribute") should be used instead of .get("v.sharedAttribute") when creating components dynamically.

Here is the updated code:

$A.createComponent(
            "c:childObject",
            {
                "sharedAttribute" : cmp.getReference("v.sharedAttribute")
            },
            function(newCmp){
                //do stuff
            }
});

Everything now works.

2
  • If you have answered your own question, please mark it as accepted.So that it can help others in the community.
    – Praveen
    Oct 17, 2016 at 18:58
  • I will when stackexchange lets me. I am seeing the error You can accept your own answer tomorrow. Oct 18, 2016 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .