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?

  • Please share the full component code showing both parent and child components code – Mohith Shrivastava Oct 17 '16 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. – Ross Bassett Oct 17 '16 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. – Mohith Shrivastava Oct 17 '16 at 17:37
4

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.

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

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.