Problems "find"ing a dynamically created child component
To attach the component(child) under a component(parent), you are doing following:
1.Create the component using $A.createComponent
2.Finding the parent component to attach the dynamic child using component.find(parentId) which returns the component instance(Aura.Component)
3.Then getting the body
of the above, then adding dynamic child to the body and setting the body
.
So, child component(dynamic) needs to be inside the body
of the parent component to be rendered in the view.
Also remember, you are attaching the dynamically created component to the body
of the <div aura:id="divDynamicChild"></div>
not to the actualy body
of the component <c:testFindDynamic_Parent>
.
So, you need to go deeper to get the actual instance of the dynamic child components that was created.
What you need to do is:
1.First, find the parent component instance using component.find("parentId").
2.Get the parent cmp body
because that's where the child cmp resides using parentCmp.get("v.body").
2.Since the child component resides in the 0th index of the parent component's body
attribute. You need to do this: parentBody[0].find("dynChildId").
var parentCmp = component.find("parentId");
var parentBody = parentCmp.get("v.body");
var childcmp = parentBody[0].find("dynChildId"); // returns the child component instance.
Now, your method buttonCallDynamicChildMethodPressed
method will look like this:
buttonCallDynamicChildMethodPressed : function(component, event, helper) {
console.log("DEBUG: (testFindDynamic_ParentController.buttonCallDynamicChildMethodPressed) Called.");
if (component.get("v.dynamicChildCreated") == true) {
console.log("DEBUG: (testFindDynamic_ParentController.buttonCallDynamicChildMethodPressed) ===== Try to find dynamic child inside the parent =====");
var divDynamicChild = component.find("divDynamicChild");
var divDynamicChildBody = divDynamicChild.get("v.body");
var dynamicChild = divDynamicChildBody[0].find("dynamicChild");
console.log("DEBUG: (testFindDynamic_ParentController.buttonCallDynamicChildMethodPressed) dynamicChild = " + dynamicChild);
console.log("DEBUG: (testFindDynamic_ParentController.buttonCallDynamicChildMethodPressed) ===== Try to find dynamic child inside dynamic div =====");
console.log("DEBUG: (testFindDynamic_ParentController.buttonCallDynamicChildMethodPressed) divDynamicChild = " + divDynamicChild);
console.log("DEBUG: (testFindDynamic_ParentController.buttonCallDynamicChildMethodPressed) dynamicChild = " + dynamicChild);
}
},
Here's an sample app:
child.cmp
<aura:component access="global">
<aura:attribute name="message" type="String"/>
<div>{!v.message}</div>
</aura:component>
parent.app
<aura:application >
<aura:dependency resource="markup://c:child" />
<ui:button label="greetings"
class="slds-button slds-button--neutral"
labelClass="label"
press="{!c.greetings}"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<p>Dynamically created button</p>
<div aura:id="test"></div>
</aura:application>
parentcontroller.js
({
doInit : function(cmp) {
$A.createComponent(
"c:child",
{
"aura:id":"test1",
"message":"Good morning"
},
function(newChild){
if (cmp.isValid()) {
var parent = cmp.find("test");
var body = parent.get("v.body");
body.push(newChild);
parent.set("v.body", body);
}
}
);
$A.createComponent(
"c:child",
{
"aura:id":"test2",
"message":"Have a nice day"
},
function(newChild){
if (cmp.isValid()) {
var parent = cmp.find("test");
var body = parent.get("v.body");
body.push(newChild);
parent.set("v.body", body);
}
}
);
},
greetings : function(cmp) {
var d = cmp.find("test").get("v.body");
console.log("child 1",d[0].find("test1").get("v.message"));
console.log("child 2",d[1].find("test2").get("v.message"));
}
})
According to the Release Notes this will be fixed with Spring'18 in Jan/Feb 18.
Find Dynamically Created Components
You can now use cmp.find() to find a component that you create dynamically with $A.createComponent().
In prior releases, a bug prevented cmp.find(), the standard method for finding a component, from working with dynamically created components. This bug has tripped up many people. Now, it just works as you would expect.