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"));
}
})
Best Answer
The issue you are facing is due to the fact that you are defining
strNewPhone
as a String (its value is enclosed by double quotes).You should define it as an Object: